使用PHP进行微信开发时,生成二维码是乱码
使用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