本文最后更新于 2024-05-03,文章内容可能已经过时。

网站跳转微信打开小程序接口PHP源码,其他平台引流到微信小程序,例如快手,抖音,网站快速跳转到小程序,高级用途:微信小程序支付,跳转小程序观看广告后网页端下发奖励。

使用前提:

1.非个人主体小程序
2.修改appid和密钥为自己小程序的,如果有参数需要传递,在下面参数位置填写
3.跳转小程序接口源码,返回的是微信url scheme

跳转小程序接口代码

<!--?php
<p>// 作者KongHen02
// 仅限非个人主体小程序</p>
<p>$appid = ""; // 小程序appid
$appkey = ""; // 小程序密钥</p>
<p>// 获取微信开放平台token
$token = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appkey);
$token = json_decode($token, true);
$token = $token["access_token"];</p>
<p>// 获取微信小程序schemeUrl
$url='https://api.weixin.qq.com/wxa/generatescheme?access_token='.$token;</p>
<p>$data = json_encode(array(
"jump_wxa" =--> array(
"path" => "/pages/index/index", // 页面地址
"query" => "" // 需要传递的参数,可留空:a=****&b=*****······
)
)
);</p>
<p>$ch = curl_init();</p>
<p>curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json, text/plain, <em>/</em>",
"Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
"Content-Type: application/json",
"Origin: https://api.weixin.qq.com",
"Content-Type: application/json"
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");</p>
<p>$result = curl_exec($ch);</p>
<p>curl_close($ch);</p>
<p>//提取link
$link = json_decode($result, true)["openlink"];</p>
<p>$data = array(
'code'=>200,
'url'=>$link
);</p>
<p>exit(json_encode($data));</p>
<p>?>