Heim > Artikel > Backend-Entwicklung > 微信公众平台开发(83) 生成带参数二维码_PHP教程
本文介绍在微信公众平台上如何使用高级接口开发生成带参数二维码的功能。
为了满足用户渠道推广分析的需要,公众平台提供了生成带参数二维码的接口。使用该接口可以获得多个带不同场景值的二维码,用户扫描后,公众号可以接收到事件推送。
目前有2种类型的二维码,分别是临时二维码和永久二维码,前者有过期时间,最大为1800秒,但能够生成较多数量,后者无过期时间,数量较少(目前参数只支持1--100000)。两种二维码分别适用于帐号绑定、用户来源统计等场景。
用户扫描带场景值二维码时,可能推送以下两种事件:
如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号,在用户扫描后会自动进入会话,微信也会将带场景值扫描事件推送给开发者。
获取带参数的二维码的过程包括两步,首先创建二维码ticket,然后凭借ticket到指定URL换取二维码。
每次创建二维码ticket需要提供一个开发者自行设定的参数(scene_id),分别介绍临时二维码和永久二维码的创建二维码ticket过程。
临时二维码请求说明
http请求URL
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
POST数据格式
<span 1</span> <span { </span><span 2</span> "expire_seconds": 1800<span , </span><span 3</span> "action_name": "QR_SCENE"<span , </span><span 4</span> "action_info"<span : { </span><span 5</span> "scene"<span : { </span><span 6</span> "scene_id": 100000 <span 7</span> <span } </span><span 8</span> <span } </span><span 9</span> }
返回格式:
<span { </span>"ticket": "gQFK8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3kweXE0T3JscWY3UTltc3ZPMklvAAIEG9jUUgMECAcAAA=="<span , </span>"expire_seconds": 1800<span }</span>
http请求URL
https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN
POST数据格式
<span { </span>"action_name": "QR_LIMIT_SCENE"<span , </span>"action_info"<span : { </span>"scene"<span : { </span>"scene_id": 1000<span } } }</span>
<span { </span>"ticket": "gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA=="<span }</span>
返回结果参数说明
程序实现
<span 1</span> <span $access_token</span> = " xDx0pD_ZvXkHM3oeu5oGjDt1_9HxlA-9g0vtR6MZ-v4r7MpvZYC4ee4OxN97Lr4irkPKE94tzBUhpZG_OvqAC3D3XaWJIGIn0eeIZnfaofO1C3LNzGphd_rEv3pIimsW9lO-4FOw6D44T3sNsQ5yXQ"<span ; </span><span 2</span> <span 3</span> <span //</span><span 临时</span> <span 4</span> <span $qrcode</span> = '{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 10000}}}'<span ; </span><span 5</span> <span //</span><span 永久</span> <span 6</span> <span $qrcode</span> = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 1000}}}'<span ; </span><span 7</span> <span 8</span> <span $url</span> = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=<span $access_token</span>"<span ; </span><span 9</span> <span $result</span> = https_post(<span $url</span>,<span $qrcode</span><span ); </span><span 10</span> <span $jsoninfo</span> = json_decode(<span $result</span>, <span true</span><span ); </span><span 11</span> <span $ticket</span> = <span $jsoninfo</span>["ticket"<span ]; </span><span 12</span> <span 13</span> <span function</span> https_post(<span $url</span>, <span $data</span> = <span null</span><span ){ </span><span 14</span> <span $curl</span> =<span curl_init(); </span><span 15</span> curl_setopt(<span $curl</span>, CURLOPT_URL, <span $url</span><span ); </span><span 16</span> curl_setopt(<span $curl</span>, CURLOPT_SSL_VERIFYPEER, <span FALSE</span><span ); </span><span 17</span> curl_setopt(<span $curl</span>, CURLOPT_SSL_VERIFYHOST, <span FALSE</span><span ); </span><span 18</span> <span if</span> (!<span empty</span>(<span $data</span><span )){ </span><span 19</span> curl_setopt(<span $curl</span>, CURLOPT_POST, 1<span ); </span><span 20</span> curl_setopt(<span $curl</span>, CURLOPT_POSTFIELDS, <span $data</span><span ); </span><span 21</span> <span } </span><span 22</span> curl_setopt(<span $curl</span>, CURLOPT_RETURNTRANSFER, 1<span ); </span><span 23</span> <span $output</span> = curl_exec(<span $curl</span><span ); </span><span 24</span> curl_close(<span $curl</span><span ); </span><span 25</span> <span return</span> <span $output</span><span ; </span><span 26</span> }
获取二维码ticket后,开发者可用ticket换取二维码图片。无须登录态即可调用。
HTTPS GET请求说明(TICKET必需UrlEncode)
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
ticket正确情况下,http 返回码是200,是一张图片,可以直接展示或者下载。
HTTP头示例如下:
<span 1</span> <span { </span><span 2</span> "url": "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA%3D%3D", <span 3</span> "content_type": "image/jpg", <span 4</span> "http_code": 200, <span 5</span> "header_size": 162, <span 6</span> "request_size": 181, <span 7</span> "filetime": -1, <span 8</span> "ssl_verify_result": 20, <span 9</span> "redirect_count": 0, <span 10</span> "total_time": 0.509, <span 11</span> "namelookup_time": 0, <span 12</span> "connect_time": 0.058, <span 13</span> "pretransfer_time": 0.343, <span 14</span> "size_upload": 0, <span 15</span> "size_download": 28497, <span 16</span> "speed_download": 55986, <span 17</span> "speed_upload": 0, <span 18</span> "download_content_length": 28497, <span 19</span> "upload_content_length": 0, <span 20</span> "starttransfer_time": 0.481, <span 21</span> "redirect_time": 0 <span 22</span> }
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQFK8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3kweXE0T3JscWY3UTltc3ZPMklvAAIEG9jUUgMECAcAAA%3d%3d
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA%3d%3d
二维码生成以后,可以在浏览器中右键另存为本地图片,但如果有很多二维码的时候,用程序来下载就比较方便。
我们使用CURL获取图片的所有信息,并将图片数据保存为一个文件。一个完整的下载代码如下:
<span $ticket</span> = "gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA=="<span ; </span><span $url</span> = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".<span urlencode</span>(<span $ticket</span><span ); </span><span $imageInfo</span> = downloadImageFromWeiXin(<span $url</span><span ); </span><span $filename</span> = "qrcode.jpg"<span ; </span><span $local_file</span> = <span fopen</span>(<span $filename</span>, 'w'<span ); </span><span if</span> (<span false</span> !== <span $local_file</span><span ){ </span><span if</span> (<span false</span> !== <span fwrite</span>(<span $local_file</span>, <span $imageInfo</span>["body"<span ])) { </span><span fclose</span>(<span $local_file</span><span ); } } </span><span function</span> downloadImageFromWeiXin(<span $url</span><span ) { </span><span $ch</span> = curl_init(<span $url</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_HEADER, 0<span ); curl_setopt(</span><span $ch</span>, CURLOPT_NOBODY, 0); <span //</span><span 只取body头</span> curl_setopt(<span $ch</span>, CURLOPT_SSL_VERIFYPEER, <span FALSE</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_SSL_VERIFYHOST, <span FALSE</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, 1<span ); </span><span $package</span> = curl_exec(<span $ch</span><span ); </span><span $httpinfo</span> = curl_getinfo(<span $ch</span><span ); curl_close(</span><span $ch</span><span ); </span><span return</span> <span array_merge</span>(<span array</span>('body' => <span $package</span>), <span array</span>('header' => <span $httpinfo</span><span )); }</span>
用户扫描带场景值二维码时,可能推送以下两种事件:
如果用户还未关注公众号,则用户可以关注公众号,关注后微信会将带场景值关注事件推送给开发者。
如果用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者。
1. 用户未关注时,进行关注后的事件推送
<span <</span><span xml</span><span ></span> <span <</span><span ToUserName</span><span ></span><span <![CDATA[</span><span gh_45072270791c</span><span ]]></span><span </</span><span ToUserName</span><span ></span> <span <</span><span FromUserName</span><span ></span><span <![CDATA[</span><span o7Lp5t6n59DeX3U0C7Kric9qEx-Q</span><span ]]></span><span </</span><span FromUserName</span><span ></span> <span <</span><span CreateTime</span><span ></span>1389684286<span </</span><span CreateTime</span><span ></span> <span <</span><span MsgType</span><span ></span><span <![CDATA[</span><span event</span><span ]]></span><span </</span><span MsgType</span><span ></span> <span <</span><span Event</span><span ></span><span <![CDATA[</span><span subscribe</span><span ]]></span><span </</span><span Event</span><span ></span> <span <</span><span EventKey</span><span ></span><span <![CDATA[</span><span qrscene_1000</span><span ]]></span><span </</span><span EventKey</span><span ></span> <span <</span><span Ticket</span><span ></span><span <![CDATA[</span><span gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==</span><span ]]></span><span </</span><span Ticket</span><span ></span> <span </</span><span xml</span><span ></span>
2. 用户已关注时的事件推送
<span <</span><span xml</span><span ></span> <span <</span><span ToUserName</span><span ></span><span <![CDATA[</span><span gh_45072270791c</span><span ]]></span><span </</span><span ToUserName</span><span ></span> <span <</span><span FromUserName</span><span ></span><span <![CDATA[</span><span o7Lp5t6n59DeX3U0C7Kric9qEx-Q</span><span ]]></span><span </</span><span FromUserName</span><span ></span> <span <</span><span CreateTime</span><span ></span>1389684184<span </</span><span CreateTime</span><span ></span> <span <</span><span MsgType</span><span ></span><span <![CDATA[</span><span event</span><span ]]></span><span </</span><span MsgType</span><span ></span> <span <</span><span Event</span><span ></span><span <![CDATA[</span><span SCAN</span><span ]]></span><span </</span><span Event</span><span ></span> <span <</span><span EventKey</span><span ></span><span <![CDATA[</span><span 1000</span><span ]]></span><span </</span><span EventKey</span><span ></span> <span <</span><span Ticket</span><span ></span><span <![CDATA[</span><span gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==</span><span ]]></span><span </</span><span Ticket</span><span ></span> <span </</span><span xml</span><span ></span>
事件代码:
以下代码判定了扫描带参数二维码的两种情形
<span private function receiveEvent($object) { $contentStr = ""; switch ($object->Event) { case "subscribe": $contentStr = "欢迎关注 "; if (isset($object->EventKey)){ $contentStr = "关注二维码场景 ".$object->EventKey; } break; case "SCAN": $contentStr = "扫描 ".$object->EventKey; break; default: break; } $resultStr = $this->transmitText($object, $contentStr); return $resultStr; }</span>
====================================================================
方倍工作室微信公众平台账号关注方法:
1. 微信通讯录-添加朋友-查找公众号-搜索“方倍工作室”
2. 微信通讯录-添加朋友-搜号码-输入“pondbaystudio”
3. 使用微信扫描下面的二维码