|
今天开发一个小程序跳转的代码,用到了这一段,记录一下!
链接做成后发布到任何地方,比如快手、qq、微信、抖音都可以直接跳转到你的微信小程序!
当在h5链接中点击跳转小程序的a标签后,会有如下提示,并不是直接跳转。在外部浏览器打开时,会提示打开微信。
新建一个token.php文件
- <?php
- class token{
- function tk(){
- $appid = '你的appid';
- $secret = '你的secret';
- $redis = new Redis();
- $redis->connect('127.0.0.1', 6379);
- $appletToken = $redis->exists("appletToken");
- if ($appletToken==0){
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
- $res = file_get_contents($url);
- $redis->set("appletToken",json_decode($res)->access_token);
- $redis->expire("appletToken","7200");
- }
- return $redis->get("appletToken");
- }
-
- function urlscheme(){
- $token = $this->tk();
- $url1 = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$token;
- $ch = curl_init();
- $data1 = array("path"=>"", "query"=>"","env_version"=>"release");
- $data = json_encode(array("expire_type"=>1,"expire_interval"=>5,"jump_wxa"=>$data1));
- curl_setopt($ch, CURLOPT_URL, $url1);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($ch);
- curl_close($ch);
- return $output;
- }
- }
复制代码
新建一个Jump.php文件实现跳转小程序
- <?php
- include_once "token.php";
- $tk = new token();
- $urlscheme = $tk->urlscheme();
- $Jump = json_decode($urlscheme)->openlink;
- header("Location: {$Jump}");
复制代码
直接访问此php文件即可实现小程序的跳转。
|
|