天天看點

PHP 擷取微信小程式接口B二維碼接口擷取微信小程式擷取access_token擷取B接口二維碼

擷取微信小程式擷取access_token

調用

$appid = '小程式的APPID';
$secret = '小程式的secret';
$token = get_access_token($appid,$secret);
           

方法

//微信小程式擷取access_token
    function get_access_token($appid,$secret)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        return $data = curl_get($url);
    }

    //模拟GET
    function curl_get($url) 
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return $data;
    }
           

擷取B接口二維碼

調用

​
$token = '擷取的token';   請參考上面方法
$scene = 'uid=6';    //要傳的參數
$page = '';          //跳轉的路徑(不填預設首頁)
$path = './1.jpg';        //二維碼儲存的路徑
get_qrcode($token,$scene,$page,$path);

​
           

方法

//獲得二維碼
    function get_qrcode($access_token,$scene,$page,$path) 
    {
        // header('content-type:image/gif');
        //header('content-type:image/png');格式自選,不同格式貌似加載速度略有不同,想加載更快可選擇jpg
        header('content-type:image/jpg');
        $uid = 6;
        $data = array();
        $data['scene'] = $scene;
        $data['page'] = $page;
        $data = json_encode($data);

        $access = json_decode($access_token,true);
        $access_token= $access['access_token'];
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
        $da = api_notice_increment($url,$data);
        file_put_contents($path, $da); 
    }
    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($curl, 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);
        if (curl_errno($ch)) {
            return false;
        }else{
            return $tmpInfo;
        }
    }
           

小程式前端擷取參數

//微信小程式js檔案中檢視scene所帶的參數
    Page({
      onLoad: function(options) {
        // options 中的 scene 需要使用 decodeURIComponent 才能擷取到生成二維碼時傳入的 scene
        var scene = decodeURIComponent(options.scene)
        consol.log(scene)
      }
    })
    //我這裡傳的參數為$data['scene'] = "uid=" 10086;
    //使用console.log(scene);得到的結果為 uid=10086
    //獲得uid 的值
var uid = scene.split("=")[1];/
           

版權聲明:本文為CSDN部落客「weixin_33835103」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_33835103/article/details/91894281