WeChat public platform development follow and remove the attention event method, public platform _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.