1

Before 2022, if you want to open the applet in H5, you only need to set the urlscheme on the platform, but starting from April 11, 2022, the URL Scheme will be valid for up to 30 days, and the permanently valid URL Scheme will no longer be supported. A short-term valid URL Scheme and a long-term valid URL Scheme are further distinguished. If it is opened outside of WeChat, users can click on the browser page to enter the applet. After each independent URL Scheme is accessed by a user, only this user can access and open the corresponding applet again, and other users cannot open the applet again through the same URL Scheme. If the URL Scheme that has been generated before this rule adjustment takes effect, if the validity period exceeds 30 days or is long-term, it will be downgraded to 30-day validity, and can only be accessed by 1 user, and the start time will be calculated from the adjustment date. 【 Official document

But the official provides a dynamic method to get the urlscheme

Proceed as follows:

  • 1: Get access_token
  • 2: Get urlscheme based on access_token
  • 3: Implement H5 jump applet according to urlscheme

    Implementation

    1. Generic calling interface method
 /**
 * curl
 */
public function httpRequest($url, $format = 'get', $data = null, $headerArray = []){
    //设置头信息
    $curl=curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if ($format == 'post') {
        //post传值设置post传参
        curl_setopt($curl, CURLOPT_POST, 1);
        if ($data) {
            $data  = json_encode($data);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($headerArray) {
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
    }
    $data=json_decode(curl_exec($curl), true);
    curl_close($curl);
    //返回接口返回数据
    return $data;
}
2. Get access_token document: [ Get access_token ]
 $appId = 'XXX';
$appsecret = 'XXX';
$data = $this->httpRequest(
    'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appId.'&secret='.$appsecret,
    'get',
    null,
    array("Content-type:application/json;","Accept:application/json")
);
$accessToken = isset($data['access_token']) ? $data['access_token'] : '';
3: Get urlscheme according to access_token [ get urlscheme ]
 $data = $this->httpRequest(
    'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$accessToken,
    'post',
    [
        'jump_wxa' => [
            'path' => "/pages/index/index",//跳转小程序地址
            'query' => ""//跳转小程序额外参数
        ],
        'expire_type' => 0
    ]
);
$openlink = isset($data['openlink']) ? $data['openlink'] : '';
4: Jump to the applet according to the urlscheme
 <script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
window.location.href = openlink;
</script>

huaweichenai
695 声望115 粉丝