I think it is more important to be prepared to become a developer than to activate the "advanced" functions of the WeChat backend in a daze, so I will put this section first.
1. Communication process of public platform
As developers, we mainly need to face two objects: WeChat server and application (website) server.
When a WeChat user sends a message to your public platform, the message is actually sent to the WeChat server first, and the WeChat server initiates another request to the website server. The website server returns the result of this request, and then the The WeChat server sends it to the WeChat client.
The entire message communication process is as follows:
Among the above 5 steps, as developers, we mainly focus on step 3. This step mainly focuses on There are actually 3 tasks:
Receive XML information from 2
Execute internal logic of the server
Organize and return XML information for 4
I will explain the above three tasks in detail later and provide a set of simple and efficient processing methods.
2. XML communication format
When users send different types of messages using the WeChat client, the website server will receive data in different formats (text, voice, pictures, etc.), data format Currently there is only XML.
For learning, I think as long as you are proficient in the simplest text types, other formats are similar. And in the following instructions, you will find that Senparc.Weixin.MP.dll can help us completely ignore these cumbersome formats and definitions.
A simple text request XML (RequestMessage) content is as follows:
<xml>
<ToUserName><![CDATA[gh_a96a4a619366]]></ToUserName>
<FromUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></FromUserName>
<CreateTime>1357986928</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[TNT2]]></Content>
<MsgId>5832509444155992350</MsgId>
</xml>
Copy after login
The official description of the corresponding node is as follows:
Parameters | Description |
---|
ToUserName | DeveloperWeChat ID |
FromUserName | Sender account (an OpenID) |
CreateTime | Message creation time (integer) |
MsgType | text |
Content | Text message content |
MsgId | Message id, 64-bit integer type |
##A simple text return XML (ResponseMessage) content is as follows:
<xml>
<ToUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></ToUserName>
<FromUserName><![CDATA[gh_a96a4a619366]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[content]]></Content>
<FuncFlag>0</FuncFlag>
</xml>
Copy after login
The official description of the corresponding node is as follows:
Parameter | Description |
ToUserName | Receiver account (received OpenID) |
FromUserName | Developer WeChat ID |
CreateTime | Message creation time |
MsgType | text |
Content | Reply message content , the length does not exceed 2048 bytes |
FuncFlag | When bit 0x0001 is flagged, the star marks the message just received. |
three, Some issues that need attention and are easily overlooked:
Each XML information has a size limit, such as text information. It is recommended that the Content content should not exceed 600 words.
In the picture above, after step 2 starts, the WeChat server has a waiting time: 5 seconds, if it is not processed within this time To step 4, then the request will be closed (including the time of data transmission). That is to say, if the time exceeds, even if the website server returns the data, the client will not receive a reply.
In text messages, it is allowed to add tags to place links. However, many friends have tested it and found that there is no problem on iOS, but the link cannot be clicked on Android. In fact, the reason is (At least so far): The Android WeChat client is strict about the tag format. Please strictly follow this format to write: content , do not use single quotes after href, and do not add other attributes.
FromUserName in the above XML node is the OpenId of the WeChat user. For the same public account, the first 6 digits of this OpenId are consistent and unique among the records of the entire public platform. of. In other words, if the same user follows two different public accounts, he will have two different OpenIds.
CreateTime uses Unix time, so if you use C#, you need to do a conversion.
Try to keep the order of XML nodes in the official API. In the past, the WeChat server used the node location to read information (node[0]) instead of the node name. Now this problem It seems to be getting better, but you still have to be careful (¥…………&%&……).
Due to this special communication method, (at least so far) all requests must be initiated from the client first. Don’t expect that just using API or SDK can actively push messages to the website server. client (of course there are other methods, such as simulated login).
4. If you want to do your job well, you must first sharpen your tools: Senparc.Weixin.MP.dll
Although the entire communication process is very simple, the official API is still Special conventions or treatments have been made for some fields, and it is also very laborious to process so many types of request data in a process-oriented manner.
For this purpose I made an open source project: Senparc.Weixin.MP, address: http://www.php.cn/
The main purpose of Senparc.Weixin.MP is to help C# Developers simplify the entire communication process, process messages in an object-oriented manner, and focus on business logic rather than cumbersome APIs.
Use Senparc.Weixin.MP and deploy it to the external network to communicate with the WeChat server. You need to prepare these environments:
VS2012 (at least VS2010 SP1);
If you need to run the MVC Demo, you need at least .NET 4.0 (install MVC 4). To run the Webforms Demo, you need at least .NET 3.5;
A domain name or IP that can be accessed using port 80, virtual host, peanut shell mapping intranet, VPS, independent server can be used.
For more Senparc.Weixin.MP SDK WeChat public platform development tutorial (2): For articles related to becoming a developer, please pay attention to the PHP Chinese website!