How to realize the development of Wechat public platform and cancel the attention to events
This article introduces the relevant knowledge of "how to realize the development of Wechat public platform and cancel the attention to events". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
The specific analysis is as follows:
When users follow or cancel their official account, Wechat will push the event to the URL filled in by the developer, making it convenient for the developer to send a welcome message to the user or unbind the account.
The following is an example of following and canceling followers on Wechat public platform. The code is as follows:
The copy code is as follows:
Define ("TOKEN", "w3note"); / / define the identification code
$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 () / / execute 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 follow the blog"; / / here are the tips sent to followers
Break
Case "unsubscribe":
$content = ""
Break
}
$result = $this- > transmitText ($object,$content)
Return $result
}
Private function transmitText ($object,$content) {
$textTpl = "
% 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 related parameters description:
Parameter description ToUserName developer WeChat account (an OpenID) CreateTime message creation time (integer) MsgType message type, eventEvent event type, subscribe (subscription), unsubscribe (unsubscription) "how to achieve Wechat public platform development attention and unfollow events" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!