获取小程序scheme码,适用于短信、邮件、外部网页等拉起小程序的业务场景。通过该接口,可以选择生成到期失效和永久有效的小程序码
/**
* 获取url scheme 接口
*/
public function getUrlScheme()
{
$access_token = $this->get_accept_access_token();
$url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $access_token;
$path = 'pages/myindex/myjoinguide';
//query 是指 传的参数
$scene = 'id=19';
$post_data = [
'jump_wxa' => [
'path' => $path,
'query' => $scene
],
'is_expire' => true,
'expire_time' => 1642780800
];
$post_data = json_encode($post_data);
// dd($post_data);
$result = $this->api_notice_increment($url, $post_data);
dd(json_decode($result));
}
//获取接单小程序access token
public function get_accept_access_token()
{
$appid = env('ACCEPT_MINI_PROGRAM_APPID');
$secret = env('ACCEPT_MINI_PROGRAM_SECRET');
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$data = $this->curl_get($url);
$dats = json_decode($data, true);
return $dats['access_token'];
}
public function api_notice_increment($url, $data)
{
$ch = curl_init();
$header = [
"Accept-Charset" => "utf-8"
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
// var_dump($tmpInfo);
// exit;
if (curl_errno($ch)) {
return false;
} else {
// var_dump($tmpInfo);
return $tmpInfo;
}
}
调用接口getUrlScheme 得到
h5页面中加上
location.href = '那个openlink链接' 即可
关于微信小程序获取url scheme的一些注意事项:
1、请求方法为post,注意jump_wxa参数的值为object;
2、Android系统不支持直接识别URL Scheme,用户无法通过Scheme正常打开小程序,开发需要使用H5页面中转,再跳转到Scheme实现打开小程序,但是并不是所有的浏览器都支持;
3.注意是在非微信浏览器打开的