借用了 appid和appsecretjson
$appId = trim('appid'); $appSecret =trim('secret'); $access_token = get_weixin_token($appId,$appSecret);
// print_r($access_token); //生成臨時二維碼 $qr_Scene = '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 10000}}}'; $qrcode_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$access_token}"; $result = postData($qrcode_url,$qr_Scene); $jsoninfo = json_decode($result,true); $ticket = $jsoninfo['ticket']; // $ticket = urlencode($ticket); $get_img = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$ticket}"; $qrcode_img = file_get_contents($get_img); // print_r($ticket); // print $qrcode_img; //打印出圖片 // header('Content-type: image/jpeg'); // echo $qrcode_img;api
$filename = './qrcode/qrcode.jpg'; $local_file = fopen($filename,'w'); $imageInfo = downloadImageFromWeixin($get_img); if(false !== $local_file){ if(false !== fwrite($local_file,$imageInfo['body'])){ fclose($local_file); } }
//獲取access_token,get方式 file_get_connects($url). function get_weixin_token($appId,$appSecret){app
$url = "https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid={$appId}&secret={$appSecret}"; $return = file_get_contents($url); $return = json_decode($return); $access_token = $return -> access_token;
// echo $access_token;exit; return $access_token;curl
}post
//post方式生成臨時二維碼 function postData($url, $data = null) { $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; }url
//將圖片數據保存爲一個文件 function downloadImageFromWeixin($url){ $ch = curl_init($url); curl_setopt($ch,CURLOPT_HEADER); curl_setopt($ch,CURLOPT_NOBODY); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $package = curl_exec($ch); $httpinfo = curl_getinfo($ch); curl_close($ch); return array_merge(array('body'=>$package),array('header'=>$httpinfo)); }code