php版h5跳转微信小程序(获取scheme)

文章介绍了如何在项目中实现基于URLScheme的微信小程序跳转,提供了前端HTML代码示例,以及后端PHP函数来获取并生成跳转所需的scheme。PHP代码包括获取access_token和调用微信API生成scheme的流程。
摘要由CSDN通过智能技术生成

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

官方文档: 获取 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();
    }

}

//贴一下返回结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

evshan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值