//生成二维码并下载 public function getQr($user_id){ $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$accessToken; $data['scene'] = $user_id; $data['width'] = 280; $res = $this->https_post($url,json_encode($data)); $file = fopen('./public/qr/'.$name.'.jpg',"w");//打开文件准备写入 fwrite($file,$res);//写入,$res为图片二进制内容 fclose($file);//关闭 } /** * 使用access_token接口,请求获得全局Access Token */ public function getAccessToken () { $wxAppConfigInfo = model("appConfig")->msg_info('id',1); $appid = $wxAppConfigInfo['appid']; $appsecret = $wxAppConfigInfo['appsecret']; $access = model('AccessToken'); $access_list = $access->msg_info('id',1); $now_time = time(); $time = $now_time - $access_list['time']; if($time > 7000){ $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); $jsoninfo = json_decode($output, true); $access_token = $jsoninfo["access_token"]; if($access_token){ $access->msg_save(array('id'=>1),array('access_token'=>$access_token,'time'=>$now_time)); } }else{ $access_token = $access_list['access_token']; } return $access_token; } public function https_post($url,$data){ $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_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); if (curl_errno($curl)) { return 'Errno'.curl_error($curl); } curl_close($curl); return $result; }