Home > WeChat Applet > WeChat Development > WeChat public platform development to obtain personalized QR code

WeChat public platform development to obtain personalized QR code

高洛峰
Release: 2017-03-03 10:06:49
Original
1732 people have browsed it

1. Function introduction

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 interfaces

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

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

  2. If the user has followed the public 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.

Create QR code ticket

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

Temporary QR code request instructions

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}}}
Copy after login

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}}}
Copy after login

Parameter description

##Parameterexpire_secondsaction_nameaction_infoscene_idReturn instructions
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}
Copy after login

Parametersticketexpire_secondsWrong Json return example:
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"}
Copy after login

Global return code description

Use the web debugging tool to debug the interface

Exchange the QR code with a ticket

After obtaining the QR code ticket, the developer can exchange the ticket with the QR code image. Please note that this interface can be called without logging in.

Request instructions

HTTP GET请求(请使用https协议)
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET
Copy after login

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
Copy after login

Returned in case of error (such as illegal ticket) HTTP error code 404.

3. Specific implementation

Still add functions based on the previous robot case, look at the code directly.

/// <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));
        }
    }
Copy after login

For more WeChat public platform development to obtain personalized QR codes, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template