使用PHP進行微信開發,通過請求二維碼時,請求頁面顯示的是亂碼,
方法①將圖片保存,在頁面返回<img>代碼的方式顯示二維碼
方法②在發起請求的php文件中設置 1 header("Content-type: image/jpeg");
1 <?php 2 3 class WeChat{ 4 private $_appid; 5 private $_appsecret; 6 private $_token; 7 8 public function __construct($appid,$appsecret,$token){ 9 $this->_appid = $appid; 10 $this->_appsecret = $appsecret; 11 $this->_token = $token; 12 13 } 14 15 16 private function _request($curl, $https=true, $method='get', $data=null){ 17 $ch = curl_init();//初始化 18 curl_setopt($ch, CURLOPT_URL, $curl);//設置訪問的URL 19 curl_setopt($ch, CURLOPT_HEADER, false);//設置不需要頭信息 20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//只獲取頁面內容,但不輸出 21 if($https){ 22 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不做服務器認證 23 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不做客戶端認證 24 } 25 if($method == 'post'){ 26 curl_setopt($ch, CURLOPT_POST, true);//設置請求是POST方式 27 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//設置POST請求的數據 28 } 29 $str = curl_exec($ch);//執行訪問,返回結果 30 curl_close($ch);//關閉curl,釋放資源 31 return $str; 32 } 33 //獲取access_token 34 private function _getAccesstoken(){ 35 $file = './accesstoken'; 36 if(file_exists($file)){ 37 $content = file_get_contents($file); 38 $content = json_decode($content); 39 if(time()-filemtime($file)<$content->expires_in) 40 return $content->access_token; 41 } 42 43 $content = $this->_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret); 44 file_put_contents($file, $content); 45 $content = json_decode($content); 46 return $content->access_token; 47 } 48 /** 49 *_getTicket():獲取ticket,用於以后換取二維碼 50 *@expires_secords:二維碼有效期(秒) 51 *@type :二維碼類型(臨時或永久) 52 *@scene:場景編號 53 **/ 54 public function _getTicket($expires_secords = 604800, $type = "temp", $scene = 1){ 55 if($type == "temp"){//臨時二維碼的處理 56 $data = '{"expire_seconds":'.$expires_secords.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';//臨時二維碼生成所需提交數據 57 return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true, "post", $data);//發出請求並獲得ticket 58 } else { //永久二維碼的處理 59 $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene.'}}}';//永久二維碼生成所需提交數據 60 return $this->_request("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->_getAccesstoken(),true, "post", $data);//發出請求並獲得ticket 61 } 62 } 63 64 /** 65 *_getQRCode():獲取二維碼 66 *@expires_secords:二維碼有效期(秒) 67 *@type:二維碼類型 68 *@scene:場景編號 69 **/ 70 public function _getQRCode($expires_secords,$type,$scene){ 71 $content = json_decode($this->_getTicket($expires_secords,$type,$scene)); 72 $ticket = $content->ticket; 73 74 $img = $this ->_request('https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket)); 75 //return $img; 76 //下面一行作為替換 77 //將生成的二維碼保存到本地 78 $file ="./".$type.$scene.'.jpg'; 79 file_put_contents($file,$img); 80 $str = "<img src=".$file.">"; 81 return $str; 82 } 83 } 84 ?>
請求代碼
1 <?php 2 define('APPID', '***'); 3 define('APPSECRET', '***'); 4 define('TOKEN', '***'); 5 6 require('./wechat.inc.php'); 7 $wechat = new WeChat(APPID,APPSECRET,TOKEN); 8 echo $wechat -> _getQRCode(604800,"temp",6);//臨時二維碼,有效期7天,場景id是6