WeChat public platform development and elimination of attention events

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

Related Article

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.