WeChat public account receiving message event message processing
As we know, there are two types of messages generated by the interaction between WeChat users and public accounts: one is ordinary messages, which are introduced in detail in the article "Ordinary message processing for WeChat public accounts"; the other is event messages. In this article, we will take a look at event messages.
Event messages are divided into six types of events: follow/unfollow, scan QR code with parameters, report geographic location, customize menu, click menu to pull message, and click menu to jump link.
Like ordinary messages, although event messages also have six cases, the XML data formats of these event messages also have several common fields:
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>
ToUserName: developer WeChat ID
FromUserName: sender WeChat ID
CreateTime: message creation time
MsgType: message type Because the above six situations are all event messages, the value of this field is the same, all of which are event
Event event type. This field exists in all six situations above, but the values are different.
For the above six situations, we look at them separately
Follow/ Unfollow Events
When a user follows or unfollows, WeChat will push this event to the URL filled in by the developer. The XML data format of following or unfollowing is as follows
<Event><![CDATA[subscribe]]></Event> //Follow
//
<Event><![CDATA[unsubscribe]]></Event> //Unsubscribe
Scan QR code event with parameters
When a user scans a QR code with a scene, the pushed events are divided into two situations
First, if the user has not followed the official account, the user can follow the official account. After following, WeChat will push the follow event with the scene value to the developer.
Second, if the user has already followed the official account, WeChat will push the scan event with the scene value to the developer.
The above two cases have the same fields, but the values of the fields are different.
1. When the user is not following, scan the event push
<Event><![CDATA[subscribe]]></Event> //Event type subscribe
<EventKey><![CDATA[qrscene_123123]]></EventKey> //Event KEY value, qrscene_ is the prefix, followed by the QR code parameter value
<Ticket><![CDATA[TICKET]]></Ticket> //QR code ticket, which can be used to exchange for a QR code image
2. Event push when the user has followed
<Event><![CDATA[SCAN]]></Event> //Event type SCAN
<EventKey><![CDATA[SCENE_VALUE]]></EventKey> //The event KEY value is a 32-bit unsigned integer, i.e. the QR code scene_id when the QR code was created
<Ticket><![CDATA[TICKET]]></Ticket> //QR code ticket, which can be used to exchange for a QR code image
Reporting Geolocation Events
After the user agrees to report the geographic location, each time the user enters a public account session, the geographic location will be reported upon entry, or once every 5 seconds after entering the session. The public account can modify the above settings on the public platform website. When reporting the geographic location, WeChat will push the reporting geographic location event to the URL filled in by the developer.
<Event><![CDATA[LOCATION]]></Event> //Event type LOCATION
<Latitude>23.137466</Latitude> //Geographic location latitude
<Longitude>113.352425</Longitude> //Geographic location longitude
<Precision>119.385040</Precision> //Geographic location precision
Custom menu events
After the user clicks on the custom menu, WeChat will push the click event to the developer. Please note that when a submenu pops up after clicking on the menu, no report will be generated.
Event push when clicking the menu to pull messages
<Event><![CDATA[CLICK]]></Event> //Event type CLICK
<EventKey><![CDATA[EVENTKEY]]></EventKey> //Event KEY value, corresponding to the KEY value in the custom menu interface
Event push when clicking the menu jump link
<Event><![CDATA[VIEW]]></Event> //Event type VIEW
<EventKey><![CDATA[www.onmpw.com]]></EventKey> //Event KEY value, link to jump to
The formats of the various messages are those we showed above.
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;
$event = $postObj->Event;
//Write different processing according to different event types
}else {
echo "";
exit;
}
}
responseMsg() //Calling this function is equivalent to opening this interface for WeChat server to call and push messages
The reception and processing of event messages is actually no different from that of ordinary messages. It mainly depends on how our business logic handles the received messages.
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
Detailed explanation of the implementation methods of SSO single sign-on in three
Publish Date:2025/03/18 Views:190 Category:NETWORK
-
Single Sign On (SSO) is not unfamiliar to us. For large systems, using SSO can reduce a lot of trouble for users. Take Baidu for example. Baidu has many subsystems - Baidu Experience, Baidu Knows, Baidu Library, etc. If we need to enter a u
What multipart/form-data does in post Upload upload files
Publish Date:2025/03/18 Views:63 Category:NETWORK
-
Everyone has used the attribute enctype="multipart/form-data" when uploading files using a form. What is the role of multipart/form-data? Let's talk about this topic. First, let's look at a case Look at the first code form action= "handl
About application/x-www-form-urlencoded
Publish Date:2025/03/18 Views:147 Category:NETWORK
-
As a data format of form, application/x-www-form-urlencoded has its own characteristics form action= "handle.php" method= "post" input type= "text" name= " uname" class= " uname" /br / input type= "text" name= "email" class=
My understanding of webservice is this
Publish Date:2025/03/18 Views:147 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
WSDL looks like this
Publish Date:2025/03/18 Views:190 Category:NETWORK
-
When I first started learning Webservice, I found that there were quite a lot of knowledge points involved, and each point could be a school of its own. Especially when I saw WSDL, I looked up information for a long time, but I was still a
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:61 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
Implementing a group chat room using socket.io
Publish Date:2025/03/18 Views:65 Category:NETWORK
-
This article will share with you an example of using socket.io to realize the function of group chat. If you want to use socket.io, you must use nodejs to implement the server, so we need to install socket.io in nodejs Install socket.io How
First contact with CGI
Publish Date:2025/03/18 Views:51 Category:NETWORK
-
Since I am a PHP programmer, I often have to build a PHP operating environment. The popular nginx+php environment is very popular, and the mode it adopts is the FastCGI method, so I spent some time to learn about FastCGI. CGI (Common Gatewa
Getting started with FastCGI
Publish Date:2025/03/18 Views:164 Category:NETWORK
-
In "First Contact with CGI", we mentioned the operating mechanisms of CGI and Server APIs, as well as their respective advantages and disadvantages. In this chapter, we will learn about FastCGI, which combines the advantages of CGI and Serv