


WeChat public platform development to obtain personalized QR code
Mar 03, 2017 am 10:06 AM1. 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
- 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.
- 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.
http請求方式:?POST URL:?https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=TOKENPOST數(shù)據(jù)格式:json POST數(shù)據(jù)例子:{"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數(shù)據(jù)格式:json POST數(shù)據(jù)例子:{"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
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協(xié)議) 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
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> ????????///?根據(jù)場景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> ????????///?創(chuàng)建臨時二維碼 ????????///?</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> ????????///?創(chuàng)建臨時二維碼 ????????///?</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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)