app跳转微信小程序
在app内或者h5 内 带参数跳转到微信小程序内实现代码
·
开发文档地址:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html
流程:
1、获取accesstoken。
2、根据文档获取一个 weixin: 开头的链接地址,直接前端 window.localtion.href 跳转即可!
注意事项:
1、accesstoken获取的时间
2、jump_wxa 参数传递的格式要跟文档一致,query只能传递 数字字符格式,有长度限制!
上代码:
private $post_url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=";
private $appid="要跳转的小程序appid";
private $appsercet = "要跳转的小程序appsercet";
private $path ="/pages/index";
private $env_version ="develop"; //正式版为"release",体验版为"trial",开发版为"develop"
//获取微信返回的跳转地址
public function get_url(){
$data = $_POST;
if(!$data['order_id']){
return show(0,"信息有误!");
}
$access_token = $this->getAsstoken();
$dataa = [
"jump_wxa" => [
"path" => $this->path,
"query" => "跳转到小程序所带的参数(只能传递数字字符)"
],
"expire_time" => 1672502399
];
$jump_url = $this->post_url.$access_token;
$res = $this->sendPOSTRequest($jump_url,json_encode($dataa));
var_dump($res);
}
//获取accesstoken
public function getAsstoken(){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsercet;
$result = file_get_contents($url);
$res = json_decode($result,true);
return $res['access_token'];
}
public function sendPOSTRequest($url, $map_json)
{
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $map_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"content-type: application/json; charset=UTF-8"
));
//执行并获取内容
$content = curl_exec($ch);
if ($content === false) {
echo 'Curl error: ' . curl_error($ch);
}
//释放curl句柄
curl_close($ch);
return $content;
}
更多推荐
已为社区贡献1条内容
所有评论(0)