微信公众号开发(十一)生成带参数二维码
公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
目前有2种类型的二维码:
1、临时二维码,是有过期时间的,最长可以设置为在二维码生成后的30天(即2592000秒)后过期,但能够生成较多数量。临时二维码主要用于帐号绑定等不要求二维码永久保存的业务场景
2、永久二维码,是无过期时间的,但数量较少(目前为最多10万个)。永久二维码主要用于适用于帐号绑定、用户来源统计等场景。
用户扫描带场景值二维码时,可能推送以下两种事件:
如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码。
每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id:整数)或者(scene_str:字符串)。
1、创建临时二维码ticket
接口:https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
当参数为整数时,tmp_ticket_id.php
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $data = '{ "expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": { "scene": { "scene_id": 100 } } }'; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;返回:
{ "ticket": "gQHv8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyRW52QnMxeEZlcGkxLXFNNWhwMS0AAgQatrxZAwSAOgkA", "expire_seconds": 604800, "url": "http://weixin.qq.com/q/02EnvBs1xFepi1-qM5hp1-" }当参数为字符串时,tmp_ticket_str.php
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $data = '{ "expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": { "scene": { "scene_str": "abcd" } } }'; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;返回:
{ "ticket": "gQEN8TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyNGZPSnRXeEZlcGkxLWhQNTFwYzEAAgQRubxZAwSAOgkA", "expire_seconds": 604800, "url": "http://weixin.qq.com/q/024fOJtWxFepi1-hP51pc1" }
2、创建永久二维码ticket
当参数为数字时,never_ticket_id,php
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $data = '{ "action_name": "QR_LIMIT_SCENE", "action_info": { "scene": { "scene_id": 111 } } }'; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;
返回:
{ "ticket": "gQH88DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyYk44LXNGeEZlcGkxMDAwMHcwM2gAAgRiubxZAwQAAAAA", "url": "http://weixin.qq.com/q/02bN8-sFxFepi10000w03h" }
<?php @header('Content-type: text/plain;charset=UTF-8'); require_once("../Utils.php"); $data = '{ "action_name": "QR_LIMIT_STR_SCENE", "action_info": { "scene": { "scene_str": "haha" } } }'; $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;返回:
{ "ticket": "gQHf8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydmhZenQzeEZlcGkxMDAwMHcwN1EAAgQGu7xZAwQAAAAA", "url": "http://weixin.qq.com/q/02vhYzt3xFepi10000w07Q" }
3、通过ticket换取二维码图片
接口:https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET,TICKET记得进行UrlEncode
该操作无需登录或授权即可调用,也就是说可以直接在浏览器中打开该链接获取图片。
例如打开以上永久二维码的id图片:
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQH88DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyYk44LXNGeEZlcGkxMDAwMHcwM2gAAgRiubxZAwQAAAAA
打开以上永久二维码str图片
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHf8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydmhZenQzeEZlcGkxMDAwMHcwN1EAAgQGu7xZAwQAAAAA
4、扫码
调用微信扫一扫功能,扫描上面的二维码,如果已关注会发送以下XML数据给开发者,并跳转到公众号,开发者此时不能回复空字符串,否则会报错。
<xml> <ToUserName> <![CDATA[gh_6b9aa8a6f1e2]]> </ToUserName> <FromUserName> <![CDATA[o4WmZ0h-4huBUVQUczx2ezaxIL9c]]> </FromUserName> <CreateTime>1505542211</CreateTime> <MsgType> <![CDATA[event]]> </MsgType> <Event> <![CDATA[SCAN]]> </Event> <EventKey> <![CDATA[haha]]> </EventKey> <Ticket> <![CDATA[gQHf8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydmhZenQzeEZlcGkxMDAwMHcwN1EAAgQGu7xZAwQAAAAA]]> </Ticket> </xml>如果用户未关注,扫描后会弹出以下界面让用户关注
点击关注后会推送给开发者如下消息,事件为subscribe关注事件,EventKey为qrscene_ +二维码参数值。
<xml> <ToUserName> <![CDATA[gh_6b9aa8a6f1e2]]> </ToUserName> <FromUserName> <![CDATA[o4WmZ0h-4huBUVQUczx2ezaxIL9c]]> </FromUserName> <CreateTime>1505542651</CreateTime> <MsgType> <![CDATA[event]]> </MsgType> <Event> <![CDATA[subscribe]]> </Event> <EventKey> <![CDATA[qrscene_haha]]> </EventKey> <Ticket> <![CDATA[gQHf8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAydmhZenQzeEZlcGkxMDAwMHcwN1EAAgQGu7xZAwQAAAAA]]> </Ticket> </xml>