本文实例讲述了微信公众平台开发关注及取消关注事件的方法。分享给大家供大家参考。具体分析如下:
用户在关注与取消关注公众号时,微信会把这个事件推送到开发者填写的URL,方便开发者给用户下发欢迎消息或者做帐号的解绑.
下面是一个微信公众平台关注和取消关注的实例,代码如下:
代码如下:define("TOKEN", "w3note");//定义识别码
$wechatObj = new wechatCallbackapiTest();//实例化wechatCallbackapiTest类
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()//执行接收器方法
{
$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 = "欢迎关注网志博客";//这里是向关注者发送的提示信息
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>
新闻热点
疑难解答