WeChat public platform development to obtain personalized QR code

Source: Internet
Author: User
Tags http error code 404
During the promotion, we can tell the other party what our public account is, and the customer can search for it and pay attention to it. The QR code provides us with great convenience. you only need to scan the QR code and pay attention to it. If you have already followed the conversation, jump into the dialog screen immediately. During our promotion, it is no longer a simple text. it can be a personalized QR code, which must be very vivid. I. Functions

During the promotion, we can tell the other party what our public account is, and the customer can search for it and pay attention to it. The QR code provides us with great convenience. you only need to scan the QR code and pay attention to it.

If you have already followed the conversation, jump into the dialog screen immediately. During our promotion, it is no longer a simple text. it can be a personalized QR code, which must be very vivid.

It provides good support for QR codes and generates QR codes for different scenarios as needed. The following describes how to obtain and use the QR code.

Note: the service number is limited and the authentication is performed. the fee is 300.

II. Related interfaces

To meet the needs of user Channel promotion analysis, the public platform provides an interface for generating a QR code with parameters. You can use this interface to obtain multiple QR codes with different scenario values. after a user scans, the public account can receive event push.

Currently, there are two types of QR codes: temporary and permanent. The former has an expiration time, up to 1800 seconds, but can generate a large number. The latter has no expiration time, the number is small (currently only 1-parameters are supported ). The two QR codes are applicable to 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:

  1. If the user has not followed the public account, the user can follow the public account, after which the event with the scene value will be pushed to the developer.

  2. If the user has followed the public account, the user will automatically enter the session after scanning, and the event with the scene value will be pushed to the developer.

The process of obtaining a QR code with parameters involves two steps. First, create a two-dimensional code ticket, and then exchange the two-dimensional code with the specified URL ticket.

Create a QR code ticket

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

Description of temporary QR Code Request

Http request method: POSTURL: https://api.weixin.qq.com/cgi-bin/qrcode/create? Access_token = TOKENPOST data format: jsonPOST data example: {"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id ": 123 }}}

Permanent QR code request description

Http request method: POSTURL: https://api.weixin.qq.com/cgi-bin/qrcode/create? Access_token = TOKENPOST data format: jsonPOST data example: {"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123 }}}

Parameter description

Parameters Description
Expire_seconds The valid time of the QR code, in seconds. The maximum value is 1800.
Action_name QR Code type. QR_SCENE is temporary and QR_LIMIT_SCENE is permanent.
Action_info QR code details
Scene_id Scenario value ID. The value of the temporary QR code is 32-bit integer type, and the maximum value of the permanent QR code is 1000.

Return description

Correct Json return results:

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

Parameters Description
Ticket The obtained two-dimensional code ticket, with which the two-dimensional code can be exchanged within the validity period.
Expire_seconds The valid time of the QR code, in seconds. The maximum value is 1800.

Example of returned error Json:

{"errcode":40013,"errmsg":"invalid appid"}

Global return code description

Use the Web Debugging Tool to debug this interface

Exchange two-dimensional codes with ticket

After obtaining the QR code ticket, the developer can exchange the ticket for the QR code image. Note that this interface can be called without logon.

Request description

Http get request (please use https protocol) https://mp.weixin.qq.com/cgi-bin/showqrcode? Ticket = TICKET

Return description

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

The HTTP header (example) is as follows:

Accept-Ranges:bytesCache-control:max-age=604800Connection:keep-aliveContent-Length:28026Content-Type:image/jpgDate:Wed, 16 Oct 2013 06:37:10 GMTExpires:Wed, 23 Oct 2013 14:37:10 +0800Server:nginx/1.4.1

In case of an error (such as illegal ticket), the HTTP error code 404 is returned.

III. implementation

Add features based on previous robot cases and view the code directly.

////// QR code manager ///Public class DimensionalCodeManager {////// Temporary QR code address ////// Use string. when format is used, the following error message is returned: string format error, because {// private const string TEMP_URL = "{\" expire_seconds \ ": 1800, \" action_name \": \ "QR_SCENE \", \ "action_info \": {\ "scene \": {\ "scene_id \": {0 }}}}";////// Solution: replace one {in the original string with two ///Private const string TEMP_JSON_DATA = "{\" expire_seconds \ ": 1800, \" action_name \ ": \" QR_SCENE \ ", \" action_info \": {{\ "scene \": {{\ "scene_id \": {0 }}}}}}}";////// Permanent QR code address ///Private const string PERMANENT_URL = "{\" action_name \ ": \" QR_LIMIT_SCENE \ ", \" action_info \ ":{{ \" scene \": {{\ "scene_id \": {0 }}}}}}}";////// Obtain the ticket URL ///Private const string GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create? Access_token = {0 }";////// Obtain the QR code URL ///Private const string GET_CODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode? Ticket = {0 }";////// Obtain ticket based on the scenario ID //////Scenario ID///Is it a temporary QR code?///
 Private static string GetTicket (int sceneID, bool isTemp) {string result = null; string data = string. empty; if (isTemp) {data = string. format (TEMP_JSON_DATA, sceneID. toString ();} else {if (sceneID> 0 & sceneID <= 1000) {data = string. format (PERMANENT_URL, sceneID);} return null;} string ticketJson = HttpUtility. getData (string. format (GET_TICKET_URL, Context. accessTok En); XDocument doc = XmlUtility. ParseJson (ticketJson, "root"); XElement root = doc. Root; if (root! = Null) {XElement ticket = root. Element ("ticket"); if (ticket! = Null) {result = ticket. Value;} return result ;}////// Create a temporary QR code //////Scenario id, int type///
 Public static string GenerateTemp (int sceneID) {string ticket = GetTicket (sceneID, true); if (ticket = null) {return null;} return HttpUtility. getData (string. format (GET_CODE_URL, ticket ));}////// Create a temporary QR code //////Scenario id, int type///
 Public static string GeneratePermanent (int sceneID) {string ticket = GetTicket (sceneID, false); if (ticket = null) {return null;} return HttpUtility. getData (string. format (GET_CODE_URL, ticket ));}}

For more articles about personalized QR codes developed on the public platform, please follow the PHP Chinese website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.