url scheme 实现跳转到小程序页面

获取小程序scheme码,适用于短信、邮件、外部网页等拉起小程序的业务场景。通过该接口,可以选择生成到期失效和永久有效的小程序码

参考文档:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html

 /**
     * 获取url scheme 接口
     */
    public function  getUrlScheme()
    {
        $access_token = $this->get_accept_access_token();
        $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $access_token;
        $path = 'pages/myindex/myjoinguide';

        //query 是指 传的参数
        $scene = 'id=19';
        $post_data = [
            'jump_wxa' => [
                'path' => $path,
                'query' => $scene
            ],
            'is_expire' => true,
            'expire_time' => 1642780800
        ];
        $post_data = json_encode($post_data);
//        dd($post_data);
        $result = $this->api_notice_increment($url, $post_data);
        dd(json_decode($result));
    }
    //获取接单小程序access token
    public function get_accept_access_token()
    {
        $appid = env('ACCEPT_MINI_PROGRAM_APPID');
        $secret = env('ACCEPT_MINI_PROGRAM_SECRET');
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
        $data = $this->curl_get($url);
        $dats = json_decode($data, true);
        return $dats['access_token'];
    }
    public function api_notice_increment($url, $data)
    {
        $ch = curl_init();
        $header = [
            "Accept-Charset" => "utf-8"
        ];
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        //     var_dump($tmpInfo);
        //    exit;
        if (curl_errno($ch)) {
            return false;
        } else {
            // var_dump($tmpInfo);
            return $tmpInfo;
        }
    }

调用接口getUrlScheme 得到

h5页面中加上

location.href = '那个openlink链接' 即可   

关于微信小程序获取url scheme的一些注意事项:

1、请求方法为post,注意jump_wxa参数的值为object;
2、Android系统不支持直接识别URL Scheme,用户无法通过Scheme正常打开小程序,开发需要使用H5页面中转,再跳转到Scheme实现打开小程序,但是并不是所有的浏览器都支持;

3.注意是在非微信浏览器打开的

 


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值