Senparc.Weixin.MP SDK WeChat public platform development tutorial (2): Become a developer

高洛峰
Release: 2017-02-22 15:28:31
Original
1500 people have browsed it

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:

Senparc.Weixin.MP SDK 微信公众平台开发教程(二):成为开发者

Among the above 5 steps, as developers, we mainly focus on step 3. This step mainly focuses on There are actually 3 tasks:

  1. Receive XML information from 2

  2. Execute internal logic of the server

  3. 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:

ParametersDescription
ToUserNameDeveloperWeChat ID
FromUserNameSender account (an OpenID)
CreateTimeMessage creation time (integer)
MsgTypetext
ContentText message content
MsgIdMessage 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:

ParameterDescriptionToUserNameReceiver account (received OpenID)FromUserNameDeveloper WeChat IDCreateTimeMessage creation timeMsgTypetextContentReply 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:

  1. Each XML information has a size limit, such as text information. It is recommended that the Content content should not exceed 600 words.

  2. 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.

  3. 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.

  4. 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.

  5. CreateTime uses Unix time, so if you use C#, you need to do a conversion.

  6. 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 (¥…………&%&……).

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

  1. VS2012 (at least VS2010 SP1);

  2. 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;

  3. 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!


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