JIYIK CN >

Current Location:Home > Learning > NETWORK >

WeChat public account receiving message event message processing

Author:JIYIK Last Updated:2025/03/16 Views:

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.

Article URL:

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 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的方法及使用案例。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial