Home  >  Article  >  WeChat Applet  >  WeChat development tutorial series (2)

WeChat development tutorial series (2)

高洛峰
高洛峰Original
2017-03-31 14:20:161888browse

The simple automatic reply message is completed. Many people don’t know how to automatically send messages to users after following them. So today I will share the implementation of automatically sending messages after successful following.

I saw that event push is also introduced in the WeChat API, so how is this attention event used? No nonsense today, let’s go straight to the code:

<?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, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);
                                    
            switch($RX_TYPE) {
                case "text" :
                    $resultStr = $this->receiveText($postObj);
                    break;
                case "event" :
                    $resultStr = $this->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->FromUserName;
                $toUsername = $object->ToUserName;
                $keyword = trim($object->Content);
                $time = time();
                                    
                //自动回复图文消息
                $textTpl = "<xml>
                             <ToUserName><![CDATA[%s]]></ToUserName>
                             <FromUserName><![CDATA[%s]]></FromUserName>
                             <CreateTime>%s</CreateTime>
                             <MsgType><![CDATA[%s]]></MsgType>
                                                     
                             <ArticleCount>3</ArticleCount>
                             <Articles>
                             <item>
                             <Title><![CDATA[测试标题1]]></Title>
                             <Description><![CDATA[测试内容啊1!]]></Description>
                             <PicUrl><![CDATA[http://weixin.comsenz-service.com/20.jpg]]></PicUrl>
                             <Url><![CDATA[http://www.baidu.com]]></Url>
                             </item>
                             <item>
                             <Title><![CDATA[哈哈,标题]]></Title>
                             <Description><![CDATA[内容标题]]></Description>
                             <PicUrl><![CDATA[http://weixin.comsenz-service.com/540.png]]></PicUrl>
                             <Url><![CDATA[http://www.discuz.com]]></Url>
                             </item>
                             <item>
                             <Title><![CDATA[12345哈哈,标题]]></Title>
                             <Description><![CDATA[798465789内容标题]]></Description>
                             <PicUrl><![CDATA[http://weixin.comsenz-service.com/20.jpg]]></PicUrl>
                             <Url><![CDATA[http://www.google.com]]></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->Event) {
            case "subscribe" :
                $contentStr = "欢迎关注社区管家!我们可以常联系了!!";
                break;
        }
        $resultStr = $this->transmitText($object, $contentStr);
        return $resultStr;
    }
    private function transmitText($object, $content, $flag = 0) {
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }
}
?>

The above is the detailed content of WeChat development tutorial series (2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn