PHP前端开发

微信开发系列教程(2)

百变鹏仔 5小时前 #前端问答
文章标签 教程

简答的自动回复消息完成了,有很多人都不知道关注自动发送给用户消息是怎么实现的,那么我今天分享一下关注成功后自动发送消息的实现。

看到微信api里面也有介绍到事件推送,那么这个关注事件是如何使用的呢? 今天不废话,直接上代码:

<?php define("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->responseMsg();class wechatCallbackapiTest {    public function responseMsg() {        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];        if(!empty($postStr)) {            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);            $RX_TYPE = trim($postObj-&gt;MsgType);                                                switch($RX_TYPE) {                case "text" :                    $resultStr = $this-&gt;receiveText($postObj);                    break;                case "event" :                    $resultStr = $this-&gt;receiveEvent($postObj);                    break;                default :                    $resultStr = "unknow msg type: " . $RX_TYPE;                    break;            }            echo $resultStr;        } else {            echo "";            exit();        }    }    private function receiveText($object) {        if (!empty($object)){                $fromUsername = $object-&gt;FromUserName;                $toUsername = $object-&gt;ToUserName;                $keyword = trim($object-&gt;Content);                $time = time();                                                    //自动回复图文消息                $textTpl = "<xml>                             <tousername></tousername>                             <fromusername></fromusername>                             <createtime>%s</createtime>                             <msgtype></msgtype>                                                                                  <articlecount>3</articlecount>                             <articles>                             <item>                             <title></title>                             <description></description>                             <picurl></picurl>                             <url></url>                             </item>                             <item>                             <title></title>                             <description></description>                             <picurl></picurl>                             <url></url>                             </item>                             <item>                             <title></title>                             <description></description>                             <picurl></picurl>                             <url></url>                             </item>                             </articles>                             <funcflag>1</funcflag>                             </xml> ";                                                        if(!empty( $keyword )){                    $msgType = "news";  //类型 news:图文消息、text:文本消息 event:事件                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);                    echo $resultStr;                }else{                    echo "Input something...";                }        }else {            echo "";            exit;        }    }    private function receiveEvent($object) {        $contentStr = "";        switch($object-&gt;Event) {            case "subscribe" :                $contentStr = "欢迎关注社区管家!我们可以常联系了!!";                break;        }        $resultStr = $this-&gt;transmitText($object, $contentStr);        return $resultStr;    }    private function transmitText($object, $content, $flag = 0) {        $textTpl = "<xml>                    <tousername></tousername>                    <fromusername></fromusername>                    <createtime>%s</createtime>                    <msgtype></msgtype>                    <content></content>                    <funcflag>%d</funcflag>                    </xml>";        $resultStr = sprintf($textTpl, $object-&gt;FromUserName, $object-&gt;ToUserName, time(), $content, $flag);        return $resultStr;    }}?&gt;