PHP微信小程式生成帶參二維碼getwxacodeunlimit
阿新 • • 發佈:2019-12-06
老闆最近有點飄了,他要在PC端的網站放一個微信小程式的二維碼,並且掃描這個二維碼以後要跳到小程式對應的房源詳情頁。
這是微信官方給出的文件,連線地址:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
方法一:生成小程式帶參二維碼
【方形碼】如圖:官方給出的文件說的很簡單,頁沒有個demo.可能是給大佬看的,像我這種一般的程式設計師看起來還是有點困難。
【系統環境】
小姐姐這邊的系統環境是Linux系統,nginx的伺服器,thinkPHP5.6的框架。
實現程式碼如下:
根據微信官方的步驟;
第一步:要先獲取呼叫API介面的accesstoken;
1 public function getAccessToken(){ 2 3 $appid = '公司的小程式appid'; 4 5 $secret = '公司的小程式sercret'; 6 7 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; 8 9 $res = json_decode($this->httpGet($url)); 10 11 $access_token = @$res->access_token; 12 13 return $access_token; 14 15 }
第二步:請求微信獲取二維碼的介面:
官方文件如圖:
小姐姐程式碼:
public function getXcxCode(){ //獲取access token $ACCESS_TOKEN = $this->getAccessToken(); //建立二維碼 $qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=".$ACCESS_TOKEN; $param = json_encode(array("path"=>"pages/detail/detail?id=5084","width"=> 150)); $result = $this->httpRequest( $qcode, $param,"POST"); $path = 'uploads/qrcode/h.jpg'; file_put_contents($path, $result); $return['status_code'] = 2000; $return['msg'] = 'ok'; $return['img'] = 'https://公司域名.com/' . $path; echo '<img src="'.$return['img'].'" />';exit; echo json_encode($return);exit; $base64_image ="data:image/jpeg;base64,".base64_encode( $result ); return '<image src='.$base64_image.'></image>'; }
有個問題是,官方給的返回的二維碼是一個base64的圖片,但是這種圖片在儲存的時候很不方便,就需要我們把base64的圖片轉化成png.或者jpg格式的圖片進行儲存。
本以為這樣子就可以大功告成,下班去約會了,竊喜中,
沒想到