Home >Backend Development >PHP Tutorial >WeChat public platform development (83) Generate QR code with parameters_PHP tutorial

WeChat public platform development (83) Generate QR code with parameters_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:461016browse

This article introduces how to use advanced interfaces to develop the function of generating QR codes with parameters on the WeChat public platform.

1. Scene QR code

In order to meet the needs of user channel promotion analysis, the public platform provides an interface for generating QR codes with parameters. Using this interface, multiple QR codes with different scene values ​​can be obtained. After the user scans them, the public account can receive event push.

There are currently two types of QR codes, namely temporary QR codes and permanent QR codes. The former has an expiration time, up to 1800 seconds, but can generate a larger number. The latter has no expiration time and a larger number. Few (currently parameters only support 1--100000). The two QR codes are respectively suitable for account binding, user source statistics and other scenarios.

When a user scans a QR code with scene value, the following two events may be pushed:

If the user has not followed the official account, the user can follow the official account. After following the official account, WeChat will push the follow event with the scene value to the developer.

If the user has followed the official account, the user will automatically enter the session after scanning, and WeChat will also push the scanning event with scene value to the developer.

The process of obtaining a QR code with parameters includes two steps. First, create a QR code ticket, and then use the ticket to the specified URL to exchange for the QR code.

2. Create QR codeticket

Every time you create a QR code ticket, you need to provide a parameter (scene_id) set by the developer. The process of creating a QR code ticket for temporary QR codes and permanent QR codes is introduced respectively.

Temporary QR code request instructions

http request URL

https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN

POST data format

<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> }

Return format:

<span {
    </span>"ticket": "gQFK8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3kweXE0T3JscWY3UTltc3ZPMklvAAIEG9jUUgMECAcAAA=="<span ,
    </span>"expire_seconds": 1800<span 
}</span>

http request URL

https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKEN

POST data format

<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>

Return result parameter description

Program implementation

<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> }

3. Use ticket to exchange for QR code

After obtaining the QR code ticket, the developer can exchange the ticket for the QR code image. It can be called without logging in.

HTTPS GET request description (TICKET requires UrlEncode)

https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET

When the ticket is correct, the http return code is 200, which is a picture that can be displayed or downloaded directly.

An example of HTTP header is as follows:

<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

4. Download the QR code

After the QR code is generated, you can right-click in the browser and save it as a local image. However, if there are many QR codes, it is more convenient to download them with a program.

We use CURL to get all the information of the image and save the image data as a file. A complete download code is as follows:

<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>

5. Scan QR code event with parameters

When a user scans a QR code with scene value, the following two events may be pushed:

If the user has not followed the official account, the user can follow the official account. After following the official account, WeChat will push the follow event with the scene value to the developer.

If the user has followed the official account, WeChat will push the scanning event with scene value to the developer.

1. When the user has not followed, push events after following the user

<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. Event push when the user has followed

<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>

Event code:

The following code determines the two situations of scanning QR codes with parameters

<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>

================================================== =====================

How to follow Fangbei Studio’s WeChat public platform account:
1. WeChat address book-add friend-search public account-search for “Fangbei Studio”
2. WeChat address book-add friend-search Number - enter "pondbaystudio"
3. Use WeChat to scan the QR code below

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/738500.htmlTechArticleThis article introduces how to use advanced interfaces to develop the function of generating QR codes with parameters on the WeChat public platform. 1. Scenario QR code In order to meet the needs of user channel promotion analysis, the public platform...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn