WeChat public platform generates QR code with scene parameters

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

 

1. Scene QR code

In order to meet the needs of user channel promotion and 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, the official account can receive event pushes.

There are currently two types of QR codes, temporary QR codes and permanent QR codes. The former has an expiration time of up to 1800 seconds, but can generate a large number of QR codes, while the latter has no expiration time and has a smaller number (currently The parameter only supports 1--100000). The two QR codes are suitable for account binding, user source statistics and other scenarios.

When a user scans a QR code with a 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, and WeChat will push the following events with scene value to the developer after following the account.

If the user has followed the official account, it will automatically enter the session after the user scans, and WeChat will also push the scan 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 exchange for the QR code at the specified URL.

 

2. Create a QR code ticket

Each time you create a QR code ticket, you need to provide a parameter (scene_id) set by the developer, and introduce the process of creating a QR code ticket for a temporary QR code and a permanent QR code.

Temporary QR Code Request Instructions

http request URL

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

POST data format

copy code
1 {
2     "expire_seconds": 1800,
3     "action_name": "QR_SCENE",
4     "action_info": {
5         "scene": {
6             "scene_id": 100000
7         }
8     }
9 }
copy code

Return format:

{
    "ticket": "gQFK8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL3kweXE0T3JscWY3UTltc3ZPMklvAAIEG9jUUgMECAcAAA==",
    "expire_seconds": 1800
}

Permanent QR Code Request Instructions

http request URL

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

POST data format

copy code
{
    "action_name": "QR_LIMIT_SCENE",
    "action_info": {
        "scene": {
            "scene_id": 1000
        }
    }
}
copy code

return format

{
    "ticket": "gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA=="
}

Submit data parameter description:

Return result parameter description

Program implementation

 

3. Exchange tickets for QR codes

After obtaining the QR code ticket, the developer can exchange the ticket for the QR code image. Can be called without being logged in.

HTTPS GET request description (TICKET required UrlEncode)

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

If the ticket is correct, the http return code is 200, which is an image that can be displayed or downloaded directly.

An example of an HTTP header is as follows:

copy code
 1 {
 2     "url": "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA%3D%3D",
 3     "content_type": "image/jpg",
 4     "http_code": 200,
 5     "header_size": 162,
 6     "request_size": 181,
 7     "filetime": -1,
 8     "ssl_verify_result": 20,
 9     "redirect_count": 0,
10     "total_time": 0.509,
11     "namelookup_time": 0,
12     "connect_time": 0.058,
13     "pretransfer_time": 0.343,
14     "size_upload": 0,
15     "size_download": 28497,
16     "speed_download": 55986,
17     "speed_upload": 0,
18     "download_content_length": 28497,
19     "upload_content_length": 0,
20     "starttransfer_time": 0.481,
21     "redirect_time": 0
22 }
copy code

The following are the URLs and generated pictures of the QR code in the two scenarios

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, but if there are many QR codes, it is more convenient to use the program to download.

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:

In this way, a picture file containing a QR code will be generated in the current directory of the program.

 

5. Scan the QR code event with parameters

When a user scans a QR code with a 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, and WeChat will push the following events with scene value to the developer after following the account.

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

1. When the user does not follow, push the event after following

copy code
<xml>
<ToUserName><![CDATA[gh_45072270791c]]></ToUserName>
<FromUserName><![CDATA[o7Lp5t6n59DeX3U0C7Kric9qEx-Q]]></FromUserName>
<CreateTime>1389684286</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
<EventKey><![CDATA[qrscene_1000]]></EventKey>
<Ticket><![CDATA[gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==]]></Ticket>
</xml>
copy code

Parameter Description

2. Event push when user has followed

copy code
<xml>
<ToUserName><![CDATA[gh_45072270791c]]></ToUserName>
<FromUserName><![CDATA[o7Lp5t6n59DeX3U0C7Kric9qEx-Q]]></FromUserName>
<CreateTime>1389684184</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[SCAN]]></Event>
<EventKey><![CDATA[1000]]></EventKey>
<Ticket><![CDATA[gQHi8DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0UweTNxNi1sdlA3RklyRnNKbUFvAAIELdnUUgMEAAAAAA==]]></Ticket>
</xml>
copy code

Parameter Description

Event code:

The following code determines two cases of scanning a QR code with parameters

copy code
private function receiveEvent($object)
{
    $contentStr = "";
    switch ($object->Event)
    {
        case "subscribe":
             $contentStr = "Welcome to follow Fangbei Studio" ;
             if ( isset ( $object -> EventKey)){
                 $contentStr = "Follow the QR code scene". $object -> EventKey;
            }
            break ;
         case "SCAN":
             $contentStr = "Scan". $object -> EventKey;
              // To achieve statistical analysis, you need to write scan events to the database, where EventKey and user OpenID can be recorded, scan time 
            break ;
         default :
             break ;      
 
    }
    $resultStr = $this->transmitText($object, $contentStr);
    return $resultStr;
}
copy code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325411943&siteId=291194637