WeChat public account receives messages and processes ordinary messages
There are two situations when WeChat users interact with public accounts: one is when WeChat users send ordinary messages to public accounts; the other is when some operations of WeChat users cause the WeChat server to notify the URL filled in by the developer through event push. This article introduces the processing of public accounts receiving ordinary messages.
The following passage is excerpted from WeChat official development documentation:
1. Regarding retrying message deduplication, it is recommended to use msgid deduplication.
2. If the WeChat server does not receive a response within five seconds, it will disconnect and re-initiate the request, retrying a total of three times. If the server cannot guarantee processing and replying within five seconds, it can directly reply with an empty string. The WeChat server will not do anything about it and will not retry. For details, please see "Send Message-Passive Reply Message".
3. In order to ensure higher security, developers can set up message encryption in the Developer Center of the official website of the public platform. After encryption is turned on, messages sent by users will be encrypted, and messages that the public account passively replies to users also need to be encrypted (but developers sending messages to users through API calls such as customer service interfaces will not be affected). For detailed instructions on message encryption and decryption, please see "Message Encryption and Decryption Instructions" .
Ordinary messages are divided into seven categories: text messages, picture messages, voice messages, video messages, short video messages, location messages and link messages.
When a WeChat user sends a message to a public account, the WeChat server will identify the type of message and send the corresponding message XML data to the developer. Taking a text message as an example, its format is as follows:
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[Content]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
The meaning of each field is as follows
ToUserNameDeveloper WeChat
IDFromUserNameSender account (an OpenID)
CreateTimeMessage creation time (integer) MsgTypeMessage
typeContentText message contentMsgIdMessage
id , 64-bit integer
After the WeChat server sends the XML data to the developer, the developer determines the type of the message based on the MsgType field. The values of MsgType for different messages are as follows:
Text message text
picture message image
voice message voice
video message video
short video message short video
location message location
link message link
For the XML data format of each message, the following five fields are available for each message, where the value of MsgType varies according to the message type.
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text/image/voice/video/shortvideo/location/link]]></MsgType>
<MsgId>1234567890123456</MsgId>
Let's look at the different fields.
Text Messages
<Content><![CDATA[Content]]></Content> //Text message content
Picture Message
<PicUrl><![CDATA[this is a url]]></PicUrl> //Picture link
<MediaId><![CDATA[media_id]]></MediaId> //Picture message media id, you can call the multimedia file download interface to pull data.
Voice Message
<MediaId><![CDATA[media_id]]></MediaId> //Voice message media id, you can call the multimedia file download interface to pull data.
<Format><![CDATA[Format]]></Format> //Voice format such as amr, speex, etc.
In addition to the above two fields, voice messages also have another field - voice recognition - which needs to be enabled in the official account before it can be used.
<Recognition><![CDATA[]]></Recognition> //Close voice recognition, the content is empty
// Open
<Recognition><![CDATA[Tencent WeChat Team]]></Recognition> //Open voice recognition, the content is the content of the recognized voice
Video Message
<MediaId><![CDATA[media_id]]></MediaId> //The media id of the video message. You can call the multimedia file download interface to pull data.
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> //The media id of the video message thumbnail. You can call the multimedia file download interface to pull data.
Small video message
<MediaId><![CDATA[media_id]]></MediaId> //The media id of the video message. You can call the multimedia file download interface to pull data.
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId> //The media id of the video message thumbnail. You can call the multimedia file download interface to pull data.
Location Message
<Location_X>23.134521</Location_X> //Geographic location latitude
<Location_Y>113.358803</Location_Y> //Geographic location longitude
<Scale>20</Scale> //Map zoom size
<Label><![CDATA[Location information]]></Label> //Geographic location information
Link Message
<Title><![CDATA[Ji Yi Blog]]></Title> //Message title
<Description><![CDATA[Ji Yi Blog - A professional technical blog]]></Description> //Message content
<Url><![CDATA[http://www.onmpw.com]]></Url> //Message link
The formats of the various messages are those we showed above.
Next, let's take a look at how developers receive these messages.
function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents("/tmp/wx.txt", $postStr); //Write the message content to the file for our own observation
//extract post data
if (!empty($postStr)){
//Write the message processing code here
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$msgType = $postObj->MsgType;
//Write different processing according to different message types
}else {
echo "";
exit;
}
}
responseMsg() //Calling this function is equivalent to opening this interface for WeChat server to call and push messages
The processing of receiving messages on WeChat public accounts is actually very simple. What we are talking about here is the processing of plain text information, as well as the safe mode and compatible mode. We will talk about it later, so here we will introduce so much.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
PHP+ajax to achieve cross-domain single sign-on
Publish Date:2025/03/16 Views:145 Category:NETWORK
-
We have previously introduced the principle of cross-domain single sign-on in "Detailed explanation of the implementation methods of three situations of SSO single sign-on" . Here we will introduce how to implement single sign-on using PHP
WeChat public account development tutorial interface configuration and identity a
Publish Date:2025/03/16 Views:124 Category:NETWORK
-
To become a developer of WeChat official account, you must first have a server that can be accessed externally. The development of WeChat official account does not limit the use of any language. Here we use PHP as the development language.
WeChat public account development tutorial to obtain access_token
Publish Date:2025/03/16 Views:65 Category:NETWORK
-
During the development of WeChat official accounts, if we want to actively push messages to the WeChat server, we must have access_token. Access_token is the only ticket for the official account. When we develop and call various WeChat inte
HTTP2 Tutorial - How to use HTTP/2 with HTTPS
Publish Date:2025/03/16 Views:84 Category:NETWORK
-
HTTPS is used to build ultra-secure networks connecting computers, machines, and servers to handle sensitive business and consumer information. HTTP/2 browser support includes HTTPS encryption, which actually complements the overall securit
微信公众号接收消息 事件消息处理
Publish Date:2016/10/18 Views:4674 Category:网络
-
事件消息分为关注/取消关注、扫描带参数的二维码、上报地理位置、自定义菜单、点击菜单拉取消息、点击菜单跳转链接共六种事件。
微信公众号接收消息 普通消息处理
Publish Date:2016/10/14 Views:2385 Category:网络
-
在微信用户和公众号产生交互的过程中会分为两种情况:一种是微信用户向公众号发送普通消息;另一种是微信用户的某些操作使得微信服务器通过事件推送的形式通知到开发者填写的
微信公众号开发教程获取access_token
Publish Date:2016/10/12 Views:4442 Category:网络
-
在微信公众号开发过程中,如果我们想要主动向微信服务器推送消息那么我们必须要有access_token。access_token 是公众号的唯一票据。这里我们介绍获取access_token的方法及使用案例。
微信公众号开发教程 接口配置与身份验证
Publish Date:2016/10/11 Views:2661 Category:网络
-
要想成为微信公众号的开发者,首先必须有可以对外访问的服务器。微信公众号的开发不限制使用什么语言,这里我们使用PHP作为开发语言。