当前位置:   article > 正文

php版h5跳转微信小程序(获取scheme)_外部h5跳转微信小程序 url scheme 动态传参数php

外部h5跳转微信小程序 url scheme 动态传参数php

项目中遇到,除了官方文档外没查到有用的资料,现在发一下自己写的

官方文档: 获取 URL Scheme | 微信开放文档

前端部分:

<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>商城小程序</title>
  </head>
  <body>
    <p style="text-align: center">跳转中.....</p>
  </body>
  <script>
    location.href = "weixin://dl/business/?t=6yJIhZkWr8t";
  </script>
</html>

php代码:

public function getScheme() {
    $user = $this->auth->getUser();
    $appid = '你得appid';
    $secret = '小程序secret';
//path是要跳转的小城页面地址,query为要携带的参数
    $body = ['jump_wxa'=>['path'=>'/pages/index/index','query'=>'pid=1']];
    $tokenurl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
    $data = file_get_contents($tokenurl);
    $data = json_decode($data, true);
    $token = $data['access_token'];
    $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $token;
    $data = $this->curl_post($url, null, $body);
    dump($data);
}
function curl_post($url,$herder,$body){
//一般框架都会自带GuzzleHttp,没有的请手动安装,或者利用curl post请求
    $client = new \GuzzleHttp\Client();
    try {
        $pram = $client->post($url,[
            'headers'=>$herder,
            'json'=>$body,
        ]);
        $content = json_decode($pram->getBody()->getContents(),true);
        return $content;
    }catch (ErrorException $exception){
        return $exception->getCode();
    }

}

//贴一下返回结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/553089
推荐阅读
相关标签
  

闽ICP备14008679号