zoukankan      html  css  js  c++  java
  • 微信公众平台开发入门教程(四)获取接收消息

    定义消息类型处理函数

    在接收到的消息中根据消息类型,定义不同的消息处理函数,

    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 "text":
                    $resultStr = $this->receiveText($postObj);
                    break;
                case "image":
                    $resultStr = $this->receiveImage($postObj);
                    break;
                case "location":
                    $resultStr = $this->receiveLocation($postObj);
                    break;
                case "voice":
                    $resultStr = $this->receiveVoice($postObj);
                    break;
                case "video":
                    $resultStr = $this->receiveVideo($postObj);
                    break;
                case "link":
                    $resultStr = $this->receiveLink($postObj);
                    break;
                case "event":
                    $resultStr = $this->receiveEvent($postObj);
                    break;
                default:
                    $resultStr = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

      原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html 

    定义并实现回复函数

    在这里,我们先全部用文本消息回复实现

        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;
        }

      原文:http://www.cnblogs.com/txw1958/p/wechat-tutorial.html 

    各种消息类型函数实现

    1、事件消息

        private function receiveEvent($object)
        {
            $contentStr = "";
            switch ($object->Event)
            {
                case "subscribe":
                    $contentStr = "欢迎关注方倍工作室";
                    break;
                case "unsubscribe":
                    $contentStr = "";
                    break;
                case "CLICK":
                    switch ($object->EventKey)
                    {
                        default:
                            $contentStr = "你点击了菜单: ".$object->EventKey;
                            break;
                    }
                    break;
                default:
                    $contentStr = "receive a new event: ".$object->Event;
                    break;
            }
            $resultStr = $this->transmitText($object, $contentStr);
            return $resultStr;
        }

    2、文本消息

        private function receiveText($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是文本,内容为:".$object->Content;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }

    3、图片消息

        private function receiveImage($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是图片,地址为:".$object->PicUrl;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }

    4、语音消息

        private function receiveVoice($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }

    5、视频消息

        private function receiveVideo($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }

    6、位置消息

        private function receiveLocation($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }

    7、链接消息

        private function receiveLink($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }

    完整代码

    /*
    方倍工作室 
    http://www.cnblogs.com/txw1958/
    */
    
    define("TOKEN", "pondbay");
    
    $wechatObj = new wechatCallbackapiTest();
    if (isset($_GET['echostr'])) {
        $wechatObj->valid();
    }else{
        $wechatObj->responseMsg();
    }
    
    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 "text":
                        $resultStr = $this->receiveText($postObj);
                        break;
                    case "image":
                        $resultStr = $this->receiveImage($postObj);
                        break;
                    case "location":
                        $resultStr = $this->receiveLocation($postObj);
                        break;
                    case "voice":
                        $resultStr = $this->receiveVoice($postObj);
                        break;
                    case "video":
                        $resultStr = $this->receiveVideo($postObj);
                        break;
                    case "link":
                        $resultStr = $this->receiveLink($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)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是文本,内容为:".$object->Content;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
    
        private function receiveImage($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是图片,地址为:".$object->PicUrl;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
    
        private function receiveLocation($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是位置,纬度为:".$object->Location_X.";经度为:".$object->Location_Y.";缩放级别为:".$object->Scale.";位置为:".$object->Label;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
    
        private function receiveVoice($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是语音,媒体ID为:".$object->MediaId;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
    
        private function receiveVideo($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是视频,媒体ID为:".$object->MediaId;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
    
        private function receiveLink($object)
        {
            $funcFlag = 0;
            $contentStr = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;
            $resultStr = $this->transmitText($object, $contentStr, $funcFlag);
            return $resultStr;
        }
    
        private function receiveEvent($object)
        {
            $contentStr = "";
            switch ($object->Event)
            {
                case "subscribe":
                    $contentStr = "欢迎关注方倍工作室";
                    break;
                case "unsubscribe":
                    $contentStr = "";
                    break;
                case "CLICK":
                    switch ($object->EventKey)
                    {
                        default:
                            $contentStr = "你点击了: ".$object->EventKey;
                            break;
                    }
                    break;
                default:
                    $contentStr = "receive a new event: ".$object->Event;
                    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;
        }
  • 相关阅读:
    模拟扑克的洗发牌
    结构体 枚举类型
    return、break和continue
    clipboard.js操作剪贴版——一些移动端交互和兼容经验
    国外主机如何ICP备案
    js文件操作之——导出Excel (js-xlsx)
    深入浅出写一个多级异步回调从基础到Promise实现的Demo
    一个考察闭包的最基础的面试题
    shell常用命令及正则辅助日志分析统计
    node-webkit笔记
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467567.html
Copyright © 2011-2022 走看看