Home >WeChat Applet >WeChat Development >WeChat public platform development to obtain personalized QR code
When promoting, we can tell the other party what our WeChat public account is, and customers can search for it and then follow it. The QR code provides us with great convenience. Just scan it and you can follow it.
If you have already followed it, jump into the conversation screen immediately. When we promote, it is no longer simple text, it can be a personalized QR code, which will definitely be very vivid.
WeChat provides good support for QR codes, and can also generate QR codes for different scenarios as needed. Below we will explain how to obtain and use QR codes.
Note: Limited to service account, and WeChat authentication is carried out, the fee is 300
## 2. Related interfacesIn 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, while the latter has no expiration time. , the number is small (currently the parameters only support 1--1000). The two QR codes are respectively suitable for account binding, user source statistics and other scenarios. When the user scans the QR code with scene value, the following two events may be pushed:http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json POST数据例子:{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}}Permanent QR code request instructions
http请求方式: POST URL: https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST数据格式:json POST数据例子:{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}Parameter description
Description | |
---|---|
The validity time of this QR code, in seconds. The maximum number does not exceed 1800. | |
QR code type, QR_SCENE is temporary, QR_LIMIT_SCENE is permanent | |
QR code details | |
Scene value ID, it is a 32-bit integer when using a temporary QR code, and the maximum value when using a permanent QR code is 1000 |
Correct Json return result:
{"ticket":"gQG28DoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0FuWC1DNmZuVEhvMVp4NDNMRnNRAAIEesLvUQMECAcAAA==","expire_seconds":1800}
Description | |
---|---|
The QR code ticket obtained, with this ticket you can Exchange the QR code within the valid period. | |
The validity time of the QR code, in seconds. The maximum number does not exceed 1800. |
{"errcode":40013,"errmsg":"invalid appid"}
Global return code description
Use the web debugging tool to debug the interface
Exchange the QR code with a ticket
Request instructions
HTTP GET请求(请使用https协议) https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
Return instructions
If the ticket is correct, the http return code is 200, which is a picture that can be displayed or downloaded directly.
HTTP header (example) is as follows:
Accept-Ranges:bytes Cache-control:max-age=604800Connection:keep-alive Content-Length:28026Content-Type:image/jpg Date:Wed, 16 Oct 2013 06:37:10 GMT Expires:Wed, 23 Oct 2013 14:37:10 +0800Server:nginx/1.4.1
Returned in case of error (such as illegal ticket) HTTP error code 404.
3. Specific implementation
/// <summary> /// 二维码管理者 /// </summary> public class DimensionalCodeManager { /// <summary> /// 临时二维码地址 /// </summary> /// 使用string.format时,报:字符串格式错误,因为其中有{ //private const string TEMP_URL = "{\"expire_seconds\": 1800, \"action_name\": \"QR_SCENE\", \"action_info\": {\"scene\": {\"scene_id\": {0}}}}"; /// <summary> /// 解决办法,将原有字符串中的一个{用两个{代替 /// </summary> private const string TEMP_JSON_DATA = "{{\"expire_seconds\": 1800, \"action_name\": \"QR_SCENE\", \"action_info\": {{\"scene\": {{\"scene_id\": {0}}}}}}}"; /// <summary> /// 永久二维码地址 /// </summary> private const string PERMANENT_URL = "{{\"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {{\"scene\": {{\"scene_id\": {0}}}}}}}"; /// <summary> /// 获取ticket的URL /// </summary> private const string GET_TICKET_URL = " https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={0}"; /// <summary> /// 获取二维码URL /// </summary> private const string GET_CODE_URL = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={0}"; /// <summary> /// 根据场景ID获取ticket /// </summary> /// <param name="sceneID">场景ID</param> /// <param name="isTemp">是否是临时二维码</param> /// <returns></returns> 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); } else { //scene_id不合法 return null; } } string ticketJson = HttpUtility.GetData(string.Format(GET_TICKET_URL,Context.AccessToken)); 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; } /// <summary> /// 创建临时二维码 /// </summary> /// <param name="sceneID">场景id,int类型</param> /// <returns></returns> 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)); } /// <summary> /// 创建临时二维码 /// </summary> /// <param name="sceneID">场景id,int类型</param> /// <returns></returns> 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 WeChat public platform development to obtain personalized QR codes, please pay attention to the PHP Chinese website for related articles!