你所在的位置:微信群>互联网推广>正文

微信公众平台常用接口-关注事件消息

原创
发布时间: 2023-07-11 10:57:45 热度: 183 作者: 李斯特 来源: 微信加 本文共 6321 字 阅读需要 22 分钟
用户关注和取消关注公众账号的时候将分别触发关注事件和取消关注事件。关注事件用户关注微信公众账号时的界面如图4-9所示,点击「关注」按钮。

用户关注和取消关注公众账号的时候将分别触发关注事件和取消关注事件。

关注事件

用户关注微信公众账号时的界面如图 4-9 所示,点击「关注」按钮,微信公众账号将收到关注事件。

图 4-9 关注微信公众账号

用户关注微信公众账号时的 XML 数据格式如下所示:

                  1372307736                  

用户取消关注微信公众账号时的界面如图 4-10 所示,点击「取消关注」按钮,微信公众账号将收到取消关注事件。

图 4-10 取消关注微信公众账号

用户取消关注微信公众账号时的 XML 数据格式如下所示:

                 1372309890                  

关注及取消关注事件消息的参数及描述如表 4-8 所示。

表 4-8 文本消息的参数及描述

关注时回复多图文

本节将关注/取消关注事件消息通过代码实现,以便读者理解。具体代码如下所示:

<?php // // 关注/取消关注事件消息 // 微信公众账号关注与取消关注事件消息 // define(「TOKEN」, 「weixin」); $wechatObj = new wechatCallbackapiTest(); if  (!isset($_GET[『echostr』])) {     $wechatObj->responseMsg(); }else{     $wechatObj->valid(); } class wechatCallbackapiTest {      public function valid()      {           $echoStr = $_GET[「echostr」];           if($this->checkSignature()){               echo $echoStr;               exit;           }      }      private function checkSignature()      {           $signature = $_GET[「signature」];           $timestamp = $_GET[「timestamp」];           $nonce = $_GET[「nonce」];           $token = TOKEN;           $tmpArr = array($token, $timestamp, $nonce);           sort($tmpArr);           $tmpStr = implode($tmpArr);           $tmpStr = sha1($tmpStr);           if($tmpStr == $signature){                return true;           }else{                return false;           }      }      public function responseMsg()      {           $postStr = $GLOBALS[「HTTP_RAW_POST_DATA」];           if (!empty($postStr)){               $postObj = simplexml_load_string($postStr, 『SimpleXMLElement』, LIBXML_NOCDATA);               $RX_TYPE = trim($postObj->MsgType);               switch ($RX_TYPE)               {                    case 「event」:                          $result = $this->receiveEvent($postObj);                          break;               }               echo $result;          }else {               echo 「」;               exit;          }      }      private function receiveEvent($object)      {           $content = 「」;           switch ($object->Event)           {                 case 「subscribe」:                        $content = array();                        $content[] = array(「Title」=>「多图文 1 标题」, 「Description」=>「」, 「PicUrl」=>「http://discuz.comli.com/weixin/weather/icon/cartoon.jpg」, 「Url」 =>「http://m.cnblogs.com/?u=txw1958」);                        $content[] = array(「Title」=>「多图文 2 标题」, 「Description」=>「」, 「PicUrl」=>「http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg」, 「Url」 =>「http://m.cnblogs.com/?u=txw1958」);                        $content[] = array(「Title」=>「多图文 3 标题」, 「Description」=>「」, 「PicUrl」=>「http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg」, 「Url」 =>「http://m.cnblogs.com/?u=txw1958」);                        break;                case 「unsubscribe」:                        $content = 「取消关注」;                        break;             }             if(is_array($content)){                 if (isset($content[0])){                     $result = $this->transmitNews($object, $content);                 }else if (isset($content[『MusicUrl』])){                     $result = $this->transmitMusic($object, $content);                 }          }else{                 $result = $this->transmitText($object, $content);          }          return $result;      }      /*       * 回复文本消息       */      private function transmitText($object, $content)      {            $textTpl = 「 <![CDATA[%s]]> <![CDATA[%s]]> %s <![CDATA[text]]> <![CDATA[%s]]> 」;              $result = sprintf($textTpl, $object->FromUserName, $object-> ToUserName, time(), $content);              return $result;      }      /*       * 回复图文消息       */      private function transmitNews($object, $arr_item)      {            if(!is_array($arr_item))                return;            $itemTpl = 「                <![CDATA[%s]]>            <![CDATA[%s]]>            <![CDATA[%s]]>            <![CDATA[%s]]>       」;             $item_str = 「」;             foreach ($arr_item as $item)                    $item_str .= sprintf($itemTpl, $item[『Title』], $item[『Description』], $item[『PicUrl』], $item[『Url』]);               $newsTpl = 「 <![CDATA[%s]]> <![CDATA[%s]]> %s <![CDATA[news]]> <![CDATA[]]> %s $item_str 」;              $result = sprintf($newsTpl, $object->FromUserName, $object-> ToUserName, time(), count($arr_item));              return $result;      }      /*       * 回复音乐消息       */      private function transmitMusic($object, $musicArray)      {            $itemTpl = 「      <![CDATA[%s]]>      <![CDATA[%s]]>      <![CDATA[%s]]>      <![CDATA[%s]]> 」;              $item_str = sprintf($itemTpl, $musicArray[『Title』], $musicArray[『Description』], $musicArray[『MusicUrl』], $musicArray[『HQMusicUrl』]);              $textTpl = 「 <![CDATA[%s]]> <![CDATA[%s]]> %s <![CDATA[music]]> $item_str 」;              $result = sprintf($textTpl, $object->FromUserName, $object-> ToUserName, time());                return $result;      } } ?>

在上面的代码中,判断消息类型为 event 而进入事件方法,再判断事件类型是否为关注或取消关注事件。当为关注事件的时候,回复一条欢迎语;当事件类型为取消关注时,直接回复空消息。因为这时用户已经取消关注,接收不到任何消息。

上述代码运行后,用户关注时,回复的多图文消息如图 4-11 所示。

图 4-11 用户关注时回复多图文消息

版权保护: 本文由 李斯特 原创,转载请保留链接: https://www.wechatadd.com/artdet/8921