This article mainly introduces the methods for paying attention to and removing attention to events on the public platform, analyzes in detail the tips for setting attention on the public platform, and provides a description of relevant parameters, it has some reference value and requires
This article mainly introduces the methods for paying attention to and removing attention to events on the public platform, analyzes in detail the tips for setting attention on the public platform, and provides a description of relevant parameters, it has some reference value and requires
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:
Parameter description
ToUserName developer ID
FromUserName sender account (an OpenID)
CreateTime message creation time (integer)
MsgType message type, event
Event type, subscribe and unsubscribe)
I hope this article will help you with php programming.