The public platform is used to develop methods for paying attention to and removing attention to events. This document describes how to pay attention to and revoke attention to events in public platform development. We will share with you how to develop and pay attention to events on the public platform.
This article describes how to pay attention to the development of the public platform and how to cancel the event. Share it with you for your reference. The specific analysis is as follows:
When a user follows and removes the public account, the event is pushed to the URL entered by the developer, so that the developer can send a welcome message to the user or unbind the account.
The following is an example of paying attention to and removing attention from the public platform. the code is as follows:
The code is as follows:
Define ("TOKEN", "w3note"); // defines the identifier
$ WechatObj = new wechatCallbackapiTest (); // instantiate the wechatCallbackapiTest class
If (! Isset ($ _ GET ["echostr"]) {
$ WechatObj-> responseMsg ();
} Else {
$ WechatObj-> valid ();
}
Class wechatCallbackapiTest
{
Public function valid ()
{
$ EchoStr = $ _ GET ["echostr"];
If ($ this-> checkSignature ()){
Echo $ echoStr;
Exit;
}
}
Public function responseMsg () // executes the receiver method
{
$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
If (! Emptyempty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ RX_TYPE = trim ($ postObj-> MsgType );
Switch ($ RX_TYPE ){
Case "event ":
$ Result = $ this-> receiveEvent ($ postObj );
Breadk;
}
Echo $ result;
} Else {
Echo "";
Exit;
}
}
Private function receiveEvent ($ object ){
$ Content = "";
Switch ($ postObj-> Event ){
Case "subscribe ":
$ Content = "Welcome to Wangzhi blog"; // The prompt message sent to the publisher
Break;
Case "unsubscribe ":
$ Content = "";
Break;
}
$ Result = $ this-> transmitText ($ object, $ content );
Return $ result;
}
Private function transmitText ($ object, $ content ){
$ TextTpl ="
%s
%s
% S
text
%s
0
";
$ Result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> $ ToUserName, time (), $ content );
Return $ result;
}
Private function checkSignature ()
{
$ Signature = $ _ GET ["signature"];
$ Timestamp = $ _ GET ["timestamp"];
$ Nonce = $ _ GET ["nonce"];
$ Token = TOKEN;
$ TmpArr = array ($ token, $ timestamp, $ nonce );
Sort ($ tmpArr, SORT_STRING );
$ TmpStr = implode ($ tmpArr );
$ TmpStr = sha1 ($ tmpStr );
If ($ tmpStr = $ signature ){
Return true;
} Else {
Return false;
}
}
}
Code parameters:
Parameters |
Description |
ToUserName |
Developer ID |
FromUserName |
Sender account (one OpenID) |
CreateTime |
Message creation time (integer) |
MsgType |
Message type, event |
Event |
Event type, subscribe and unsubscribe) |
I hope this article will help you with php programming.
Examples in this article describe how to pay attention to the development of the public platform and how to cancel the event of interest. Share it with you...