微信消息管理之被动回复用户消息

栏目: 编程语言 · XML · 发布时间: 6年前

内容简介:当用户发送消息给公众号时(或某些特定的用户操作引发的事件推送时),会产生一个POST请求,开发者可以在响应包(Get)中返回特定XML结构,来对该消息进行响应(现支持回复文本、图片、图文、语音、视频、音乐)。严格来说,发送被动响应消息其实并不是一种接口,而是对微信服务器发过来消息的一次回复。首先处理用户发过来的消息,然后根据消息服务器做出相应的响应。1.文本消息

当用户发送消息给公众号时(或某些特定的用户操作引发的事件推送时),会产生一个POST请求,开发者可以在响应包(Get)中返回特定XML结构,来对该消息进行响应(现支持回复文本、图片、图文、语音、视频、音乐)。严格来说,发送被动响应消息其实并不是一种接口,而是对微信服务器发过来消息的一次回复。

首先处理用户发过来的消息,然后根据消息服务器做出相应的响应。

public function responseMsg(){
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];   //用户端发来的数据
    
    //if (!empty($postStr)){
    /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
       the best way is to check the validity of xml by yourself */
    libxml_disable_entity_loader(true);
    //通过simplexml进行数据解析,转换为对象
    $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);   
    
    
    switch($postObj->MsgType){              //用户发送的消息类型
         case "event": 
            $this->_doEvent($postObj); 
            break;
         case "text": 
            $this->_doText($postObj); 
            break;
         case "image": 
            $this->_doImage($postObj); 
            break;
         case "voice": 
            $this->_doVoice($postObj); 
            break;
         case "music": 
            $this->_doMusic($postObj); 
            break;
        case "location": 
            $this->_doLocation($postObj); 
            break;
         default: 
            break;
    }  
           
}

1.文本消息

//文本消息
private function _doText($postObj){
    
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $keyword = trim($postObj->Content);
    $time = time();
    $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
                </xml>";             
    $msgType = "text";
    
    switch($keyword){
        case '初音科技':
            $contentStr = "提供一流的技术支持!!";
            break;
        case 'PHP':
            $contentStr = "最美的语言!!";
            break;
        case '图文':
            $newsArr = array(
                    array(
                    'Title'=>'初音科技,专为高端网络定制',
                    'Description'=>'初音科技,做一个品牌影响一个行业!',
                    'PicUrl'=>'http://edu.zbxiaochengxu.com/o_1b9pjfv6i1111gp69pp1m45sd7q.jpg',
                    'Url'=>'http://mp.weixin.qq.com/s?__biz=MzA4NTkzMzI0Ng==∣=2652036751&idx=2&sn=641c21e3831c094cdaf40e115c9eaee6&scene=0#wechat_redirect',
                ),
                array(
                    'Title'=>'初音科技,专为高端网络定制',
                    'Description'=>'初音科技,做一个品牌影响一个行业!',
                    'PicUrl'=>'http://qfenxiang.chuyin.net.cn/weixinopen/images/0.jpg',
                    'Url'=>'http://mp.weixin.qq.com/s?__biz=MzA4NTkzMzI0Ng==∣=2652036751&idx=2&sn=641c21e3831c094cdaf40e115c9eaee6&scene=0#wechat_redirect',
                )
            );
            $this->_replayNews($postObj,$newsArr); 
            break;
        default:
            $contentStr = "启军测试微信公众号响应!!";
            break;
    } 
    
    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    echo $resultStr;    
}

2.图片消息

//回复图片消息
private function _doImage($postObj,$mediaId){
    
    //$contentStr = "您发送的是图片信息!消息类型为:".$postObj->PicUrl."\nMediaId为:".$postObj->MediaId;
    if( $postObj->MsgType == 'image'){
        $mediaId = $postObj->MediaId;
    }
    
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $time = time();
    $imageTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Image>
                <MediaId><![CDATA[%s]]></MediaId>
                </Image>
                </xml>";
    $msgType = "image";
    
    //$this->_replayText($postObj,$contentStr);
    $resultStr = sprintf($imageTpl,$fromUsername,$toUsername,$time,$msgType,$mediaId);
    echo $resultStr;
}

3.语音消息

//回复语音消息
private function _doVoice($postObj,$mediaId){
    
    
    if( $postObj->MsgType == 'voice'){
        //$mediaId = $postObj->MediaId;
        $contentStr = "您发送的是语音信息!\n你说的是:".$postObj->Recognition;
        $this->_replayText($postObj,$contentStr);
        exit();
    }
    
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $time = time();
    $voiceTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Voice>
                <MediaId><![CDATA[%s]]></MediaId>
                </Voice>
                </xml>";
    $msgType = "voice";
    
    $resultStr = sprintf($voiceTpl,$fromUsername,$toUsername,$time,$msgType,$mediaId);
    echo $resultStr;
    
}

4.音乐消息

//音乐消息

private function _doMusic($postObj){

$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$musicTpl = "<xml>
            <ToUserName><![CDATA[%s]]></ToUserName>
            <FromUserName><![CDATA[%s]]></FromUserName>
            <CreateTime>%s</CreateTime>
            <MsgType><![CDATA[%s]]></MsgType>
            <Music>
            <Title><![CDATA[%s]]></Title>
            <Description><![CDATA[%s]]></Description>
            <MusicUrl><![CDATA[%s]]></MusicUrl>
            <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
            </Music>
            </xml>";             
$msgType = "music";
$title = "还魂门";
$desc = "电视剧<老九门>主题曲";
$url = "http://qfenxiang.chuyin.net.cn/weixinopen/musics/music.mp3";
$hqurl = "http://qfenxiang.chuyin.net.cn/weixinopen/musics/music.mp3";
    
$resultStr = sprintf($musicTpl,$fromUsername,$toUsername,$time,$msgType,$title,$desc,$url,$hqurl,$contentStr);
echo $resultStr;

}

5.图文消息

//[封装]图文回复
private function _replayNews($postObj,$newsArr){
    
    if( !is_array($newsArr) || sizeof($newsArr)<1 ){
        $newsArr = array(
            array(
                'Title'=>'全民创业蒙的就是你,来一盆冷水吧!',
                'Description'=>'全民创业已经如火如荼,然而创业是一个非常自我的过程,它是一种生活方式的选择。从外部的推动有助于提高创业的存活率,但是未必能够提高创新的成功率。',
                'PicUrl'=>'http://yun.topthink.com/Uploads/Editor/2015-07-30/55b991cad4c48.jpg',
                'Url'=>'http://www.topthink.com/topic/11991.html',
            )
        );
    }
    
    $itemStr = '';
    foreach($newsArr as $item){
        $newsData = '<item>
                        <Title><![CDATA[%s]]></Title> 
                        <Description><![CDATA[%s]]></Description>
                        <PicUrl><![CDATA[%s]]></PicUrl>
                        <Url><![CDATA[%s]]></Url>
                    </item>';
        $itemStr .= sprintf($newsData,$item['Title'],$item['Description'],$item['PicUrl'],$item['Url']);
    }
    
    $fromUsername = $postObj->FromUserName;
    $toUsername = $postObj->ToUserName;
    $time = time();
    $newsTpl = '<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <ArticleCount>%s</ArticleCount>
                <Articles>'.$itemStr.'</Articles>
                </xml>';
    $msgType = 'news';
    $count = count($newsArr);
    
    $resultStr = sprintf($newsTpl,$fromUsername,$toUsername,$time,$msgType,$count,$str);
    echo $resultStr;    
}

微信消息管理之被动回复用户消息


以上所述就是小编给大家介绍的《微信消息管理之被动回复用户消息》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Four

The Four

Scott Galloway / Portfolio / 2017-10-3 / USD 28.00

NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

SHA 加密
SHA 加密

SHA 加密工具