The example in this article describes the method of developing follow and unfollow events on the WeChat public platform. Share it with everyone for your reference. The specific analysis is as follows:
When a user follows or unfollows a public account, WeChat will push this event to the URL filled in 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 following and unfollowing on the WeChat public platform. The code is as follows:
define("TOKEN", "w3note");//Define 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 is the prompt message 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 parameter description:
参数 |
描述 |
ToUserName |
开发者微信号 |
FromUserName |
发送方帐号(一个OpenID) |
CreateTime |
消息创建时间 (整型) |
MsgType |
消息类型,event |
Event |
事件类型,subscribe(订阅)、unsubscribe(取消订阅) |
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/932071.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932071.htmlTechArticleWeChat public platform develops the method of following and unfollowing events. The example of the public platform in this article describes the development of following and unfollowing events on WeChat public platform. How to unfollow an event. Share it with everyone for everyone...