微信小程式生成帶引數的二維碼
阿新 • • 發佈:2019-01-07
微信官方說明
PHP程式碼實現
需要呼叫的公共函式
function https_request($url,$data = null){ if(function_exists('curl_init')){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }else{ return false; } }
封裝了兩個方法
// 傳送access_token
public function getAccessToken($appid,$secret,$grant_type){
if (empty($appid)||empty($secret)||empty($grant_type)) {
return '引數錯誤';
}
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type={$grant_type}&appid={$appid}&secret={$secret}";
if (S('wx_token')) {
$token = S('wx_token');
return 'success';
}
$json = https_request($url);
$data=json_decode($json,true);
if (empty($data['access_token'])) {
return $data;
}
S('wx_token',$data,3600);
return 'success';
}
// 獲取帶引數的二維碼
// 獲取小程式碼,適用於需要的碼數量極多的業務場景。通過該介面生成的小程式碼,永久有效,數量暫無限制。
public function getWXACodeUnlimit($access_token,$path='',$width=430){
if (empty($access_token)||empty($path)) {
return 'error';
}
// https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
$url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={$access_token}";
$data = array();
$data['path'] = $path;
//最大32個可見字元,只支援數字,大小寫英文以及部分特殊字元:!#$&'()*+,/:; [email protected]_~,其它字元請自行編碼為合法字元(因不支援%,中文無法使用 urlencode 處理,請使用其他編碼方式)
$data['width'] = $width;
//二維碼的寬度,預設為 430px
$json = https_request($url,json_encode($data));
return $json;
}
這裡是核心的程式碼邏輯
public function qrcode(){ $wechat = C('wechat'); $SupermarketModel = D('Supermarket'); $superId = I('request.id','','intval'); $w = array(); $w['id'] = $superId; $superDefault = $SupermarketModel->where($w)->find(); if (empty($superDefault)) { ajax_return(false,'未找到相關資訊'); } $res = $SupermarketModel->getAccessToken($wechat['appId'],$wechat['appSecret'],'client_credential'); if ($res == 'success') { $token = S('wx_token'); $access_token = $token['access_token']; }else{ ajax_return(false,$res); } if (empty($access_token)) { ajax_return(false,'access_token為空,無法獲取二維碼'); } $path = 'pages/index/index?super='.$superId; $width = 430; $res2 = $SupermarketModel->getWXACodeUnlimit($access_token,$path,$width); // var_dump($res2); //將生成的二維碼儲存到本地 // $file ="/Uploads/".substr($path,strripos($path,"?")+1).".jpg"; $file ="Uploads/".$superId.".jpg"; file_put_contents('./'.$file,$res2); if (file_exists($file)) { ajax_return(true,'','/'.$file); }else{ ajax_return(false); } }
重要的也是最坑的
1.在獲取二維碼時,需要post向介面提交資料,只是說返回的是object。沒有直接在檔案中說明提交的post資料也需要是json物件。傳送的既然不是陣列,那麼在curl請求就不能寫成
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post))
必須寫成
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
2.最坑的就是二維碼獲取成功後的處理。只要邏輯沒有問題,獲取引數二維碼成功後,介面地址會直接返回如下字串。
這表明你已經獲取成功了。該如何把它變為一張圖片呢,百度了好多,大家都沒仔細描述這一步,還有同樣的做到這裡不會做了,後期更新。自己來吧:就是利用php自帶的檔案寫入函式,把這些字元寫入到圖片格式的檔案中就成功了。
$file ="Uploads/qrcode.jpg";
file_put_contents('./'.$file,$res2);