獲取微信小程序scheme碼,適用於短信、郵件、外部網頁等拉起小程序的業務場景


 

這里屬於HTTPS調用    貼它!

注意:access_token參數在URL中體現,其它參數寫在POST請求的body中!

<?php

/**
 * 獲取access_token
 * $appid:開發者ID(AppID)
 * $appsecret:開發者密碼(AppSecret)
 *
 * 返回access_token、有效時間
 */
function wx_xcx_code($appid, $appsecret){
    $access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    $access_info = json_decode(file_get_contents($access_url),true);
    return $access_info;
}


/**
 * 拼接接口及請求參數  調用POST請求 返回結果
 */
function  getUrlScheme(){
    $access_token = wx_xcx_code("開發者ID-AppID", "開發者密碼-AppSecret");
    $access_token = $access_token['access_token']; // 加個判斷,是否獲取成功
    $url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $access_token;
    $path = '/pages/index/index';

    //query  跳轉傳遞的參數
    $scene = 'id=19';
    $post_data = [
        'jump_wxa' => [
            'path' => $path,
            'query' => $scene
        ],
        'is_expire' => true,
        'expire_time' => 1670814625
    ];
    $post_data = json_encode($post_data);
    $result = api_notice_increment($url, $post_data);
    print_r(json_decode($result));
}

/**
 * 發起 POST 請求微信接口
 * https://api.weixin.qq.com/wxa/generatescheme?access_token=ACCESS_TOKEN
 *
 * 返回結果
 */
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);
    if (curl_errno($ch)) {
        return false;
    } else {
        // var_dump($tmpInfo);
        return $tmpInfo;
    }
}


// 調用 打印結果
$ok = getUrlScheme();
print_r($ok);
?>

 

參考資料:

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/url-scheme/urlscheme.generate.html#method-http

https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-scheme.html

 

文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允許發布到網站首頁文字少的博文不允


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM