Public platform development and follow-up events removal methods, 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:
Copy codeThe 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 = "<xml>
<ToUserName> <! [CDATA [% s]> </ToUserName>
<FromUserName> <! [CDATA [% s]> </FromUserName>
<CreateTime> % s </CreateTime>
<MsgType> <! [CDATA [text]> </MsgType>
<Content> <! [CDATA [% s]> </Content>
<FuncFlag> 0 </FuncFlag>
</Xml> ";
$ 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.