微信公众号开发笔记3--生成带参数二维码
每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id),分别介绍临时二维码和永久二维码的创建二维码ticket过程。
临时二维码请求说明
http请求URL
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
POST数据格式
{ "expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": { "scene": { "scene_id": 100000 } } }
永久二维码请求说明
http请求URL
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
POST数据格式
{ "action_name": "QR_LIMIT_SCENE", "action_info": { "scene": { "scene_id": 1000 } } }
完整php代码:别忘了替换你自己的Appid和access_token
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php //创建二维码 require_once "lib.php"; require_once "downloadfile.php"; //肉盾 define("APPID", "wxeebd266"); //APPID define("APPSECRET", "f6e52");//APPSECRET $token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET; //获取ACCESS_TOKEN请求的URL地址; $res = file_get_contents($token_access_url); $result = json_decode($res,true);//将获取的返回Json值转换为数组格式 $access_token = $result['access_token'];//将获取的access_token保存到临时变量中。*/ //$access_token = "r9IM7qFARYaY7gYOPQLHujhnAkUFS4aqxhMuFao_icIHqgluuCR1Tgnq2S3BIqhlXp1pLY705xsIFhyMZeBAEqIKez9z18ivFT1aLFF40xQV6fJq6ngvwGA0C5ZEcc5UxQv_l4Szr35mrZS01yZUag"; echo $access_token; define("ACCESS_TOKEN", $access_token); $get_msg_url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".ACCESS_TOKEN; $rand_area = rand(1, 1000); $expire_seconds = rand(1, 300); //创建临时的ticket $post_er_array = array( "expire_seconds" => $expire_seconds, //临时二维码有效时间 "action_name"=>"QR_SCENE", "action_info"=>array( "scene"=>array( "scene_id"=> $expire_seconds ) ) ); $post_json = json_encode($post_er_array); //$post_json = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}'; //创建永久的ticket /*$post_er_array = array( "action_name"=>"QR_LIMIT_SCENE", "action_info"=>array( "scene"=>array( "scene_id"=> $rand_area ) ) ); $post_json = json_encode($post_er_array); p($post_json);*/ //$post_json = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}'; $result = postMsg($get_msg_url, $post_json); $arr = json_decode($result, true); //获取ticket的值 //p($arr); $ticket = $arr["ticket"]; p($ticket); define("TICKET", urlencode($ticket)); //拉取二维码的URL地址 $get_erweima_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".TICKET; p($get_erweima_url); //将这里的地址链接直接黏贴到浏览器地址栏访问即可查看到生成的二维码图片 echo downloadfile($get_erweima_url); //开启即可下载生成的二维码图片
上面的代码会生成一个访问二维码的地址,但是下载我找不到路径在哪里...........
downloadfile.php
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?php function downloadfile($url, $filename="", $timeout=60) { $filename = empty($file) ? pathinfo($url,PATHINFO_BASENAME) : $filename; $result = pathinfo($url); $time = date("Y-m-d_H_i_s", time()); // $filename = "./catchFile/".$time.'.'.$result['extension']; $filename = "./catchFile/".$time.'.jpg'; $dir = pathinfo($filename,PATHINFO_DIRNAME); !is_dir($dir) && @mkdir($dir,0755,true); //var_dump($url); $url = str_replace(" ","%20",$url); //echo "<br />"."asdfasdf"."<br />"; if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //$size = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); //var_dump($size); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //抓取https 链接的设置 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $temp = curl_exec($ch); if(@file_put_contents($filename, $temp) && !curl_error($ch)) { return $filename; } else { return false; } }else { $opts = array( "http"=>array( "method"=>"GET", "header"=>"", "timeout"=>$timeout) ); $context = stream_context_create($opts); if(@copy($url, $file, $context)) { //$http_response_header return $file; } else { return false; } } }
树林美丽、幽暗而深邃,但我有诺言尚待实现,还要奔行百里方可沉睡。 -- 罗伯特·弗罗斯特