1. 程式人生 > >微信公眾號開發(三) -- 生成臨時二維碼

微信公眾號開發(三) -- 生成臨時二維碼

1.臨時二維碼和長期二維碼是由 scene_id 的值區分的 scene_id=1是長期
2.通過掃描二維碼關注的使用者 使用者資訊場景值(qr_scene) 為scene_id的值
3.此處只做臨時二維碼的說明

//生成臨時二維碼
function getTimeQrCode(){
    //1、獲取ticket票據
    //全域性票據access_token 網頁授權access_token  微信js_SDK jsapi_ticket
    $appid = ''; //微信支付申請對應的公眾號的APPID
    $appsecret = ''; ////微信支付申請對應的公眾號的APP Key
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
    $res = http_curl($url,'get','json');
    $access_token = $res['access_token'];
    $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
    $postArr = array(
        'expire_seconds' => 604800, //二維碼過期時間 不填預設30s
        'action_name' => "QR_SCENE", //場景
        'action_info' => array(
        'scene' =>array('scene_id' => 2000,),
    ),
    );
    $postJson = json_encode($postArr);
    $res = http_curl($url,'post','json',$postJson);
    $ticket = $res['ticket'];

    //2、使用ticket獲取二維碼圖片
    $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
    echo "<img src='".$url."' />";
}

getTimeQrCode();

/*
*$url 介面url string
*$type 請求型別 string
*$res 返回資料型別 string
*%$arr post 請求引數 string
*/
function http_curl($url,$type='get',$res='json',$arr=''){
    //1.例項化curl
    $ch = curl_init();
    //2.設定curl引數
    curl_setopt($ch,CURLOPT_URL,$url);//要訪問的url地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);//對認證證書的來源檢查
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//從證書中檢查SSL加密演算法是否存在
    if($type=='post'){
        curl_setopt($ch, CURLOPT_POST, 1);//傳送一個常規的POST請求
        curl_setopt($ch, CURLOPT_POSTFIELDS,$arr);//post提交的資料包
    }
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//獲取的資訊以檔案流的形式返回

    //3.採集

    $output = curl_exec($ch);//執行操作
    if($res=='json'){
        if(curl_errno($ch)){
            return curl_error($ch);
        }else{
            return json_decode($output,true);
        }
    }
    //4.關閉
    curl_close($ch);
}