1.带参数的二维码介绍及使用场景介绍
- 为了满足用户渠道推广分析和用户帐号绑定等场景的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。不同的参数,可以相应不同的操作
- 使用场景:任何需要区分渠道的需求都适用
2.微信带参数的二维码分为两种:
- 临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
- 永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
3.生成带参数二维码接口调用实例(这里生成的是永久性的二维码,临时性的差不多,只是改下参数,具体看微信开发文档)
1 <?php 2 $appId = "sdasdfasd"; 3 $appSecret = "asdfasdfadfasdfas"; 4 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}"; 5 6 // $output = getToken($url); 7 // $token = (array)json_decode($output); 8 // $accessToken = $token['access_token']; 9 // echo $accessToken; 10 11 $accessToken = "adsfasdfasdfasdfasdfasdfasdfasdfadfasdfasdfasdfasdafdfasdfasdfasd"; 12 13 $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 11}}}'; 14 15 //获取二维码的ticket的url 16 $url_get_Qcode_ticket = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$accessToken}"; 17 18 $code = (array)json_decode(getShort($data, $url_get_Qcode_ticket)) ; 19 // echo $code['ticket']; 20 21 22 $ticket = $code['ticket']; 23 24 //获取二维码的url 25 $url_get_Qcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$ticket}"; 26 27 //运行程序会输出一个二维码图片 28 echo getToken($url_get_Qcode); 29 30 function getShort($data, $url) { 31 $ch = curl_init(); 32 curl_setopt($ch, CURLOPT_URL, $url); 33 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 34 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 35 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 36 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)"); 37 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 38 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 39 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 40 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 41 42 $tmpInfo = curl_exec($ch); 43 44 if (curl_errno($ch)){ 45 return curl_error($ch); 46 } 47 48 curl_close($ch); 49 return $tmpInfo; 50 } 51 52 function getToken($url) { 53 $ch = curl_init(); 54 curl_setopt($ch, CURLOPT_URL, $url); 55 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 56 curl_setopt($ch, CURLOPT_HEADER, 0); 57 curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)"); 58 curl_setopt($ch, CURLOPT_ENCODING, "gzip");//加入gzip解析 59 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 60 $output = curl_exec($ch); 61 curl_close($ch); 62 63 return $output; 64 }
运行如上代码后,浏览器会输出个图片格式的二维码,某些情况下回出现乱码,此时更换浏览器试试,比如IE。。。
4.扫描带参数的二维码微信公众平台响应实例