微信開發之消息回覆

a、純文本回復

上代碼:

 public function index()
    {

        //將timestamp,nonce,token按字典序排序
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        $token = 'weixin';
        $signature = $_GET['signature'];
        //將排序之後的三個參數拼接之後用sha1加密
        $array = array($timestamp, $nonce, $token);
        sort($array);
        $tmpstr = implode('', $array);
        $tmpstr = sha1($tmpstr);
        //將加密後的字符串與sianature進行對比,判斷請求是否來自於微信
        if ($tmpstr == $signature && $_GET['echostr']) {//第一次接入微信Api
            echo $_GET['echostr'];
            exit;
        } else {
            $this->responseMsg();
        }
    }

    public function responseMsg()
    {
        //接收到的用戶消息格式<xml>
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
//        接收到的消息格式
//          <xml>
//          <ToUserName><![CDATA[toUser]]></ToUserName>
//          <FromUserName><![CDATA[FromUser]]></FromUserName>
//          <CreateTime>123456789</CreateTime>
//          <MsgType><![CDATA[event]]></MsgType>
//          <Event><![CDATA[subscribe]]></Event>
//          </xml>
        //把接收到的xml格式數據包轉化爲對象格式
        $postObj = simplexml_load_string($postArr);
//        被動回覆用戶消息格式
//            <xml>
//            <ToUserName><![CDATA[toUser]]></ToUserName>
//            <FromUserName><![CDATA[fromUser]]></FromUserName>
//            <CreateTime>12345678</CreateTime>
//            <MsgType><![CDATA[text]]></MsgType>
//            <Content><![CDATA[你好]]></Content>
//            </xml>
        if (strtolower($postObj->MsgType) == 'event') {//判斷是否爲訂閱的事件推送
            if (strtolower($postObj->Event) == 'subscribe') {//判斷是否爲關注的事件推送
                $toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                $time = time();
                $msgType = 'text';
                $content = '你好:'.$postObj->FromUserName.'!歡迎關注我們的微信公衆號:'.$postObj->ToUserName;;
                $template = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
                $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                echo $info;
            }
        }elseif(strtolower($postObj->MsgType == 'text')){
            if($postObj->Content == 'hello'){
                $content = '我愛你!';
            }elseif($postObj->Content == 'baidu'){
                $content = '<a href="http://www.baidu.com"> 百度</a>';
            }
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'text';
            $template = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            </xml>";
            $info = sprintf($template,$toUser,$fromUser,$time,$msgType,$content);
            echo $info;
            }
        }
    }

@$ b、單圖文回覆與多圖文::區別,foreach個數

  $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;
            $time = time();
            $msgType = 'news';
            $arrdata = [
                [
                    'Title' => '端午節促銷',
                    'Description' => '慶祝端午節,只要99只要99,就這一天',
                    'PicUrl' => 'http://hiphotos.baidu.com/zhixin/abpic/item/d1571724ab18972bbb8d648ce4cd7b899f510a85.jpg',
                    'Url' => 'http://www.baidu.com'
                ],
                [
                    'Title' => '元宵節促銷',
                    'Description' => '慶祝元宵節,只要9.9只要9.9,就這一天',
                    'PicUrl' => 'https://www.baidu.com/img/bd_logo1.png',
                    'Url' => 'http://www.mnxiao.top'
                ],
                [
                    'Title' => '元宵節促銷',
                    'Description' => '慶祝元宵節,只要9.9只要9.9,就這一天',
                    'PicUrl' => 'https://www.baidu.com/img/bd_logo1.png',
                    'Url' => 'http://www.mnxiao.top'
                ],
            ];
            $template = "<xml>
                                <ToUserName><![CDATA[%s]]></ToUserName>
                                <FromUserName><![CDATA[%s]]></FromUserName>
                                <CreateTime>%s</CreateTime>
                                <MsgType><![CDATA[%s]]></MsgType>
                                <ArticleCount>" . count($arrdata) . "</ArticleCount>
                                <Articles>";
            foreach ($arrdata as $value) {
                $template .= "<item>
                                <Title><![CDATA[" . $value['Title'] . "]]></Title>
                                <Description><![CDATA[" . $value['Description'] . "]]></Description>
                                <PicUrl><![CDATA[" . $value['PicUrl'] . "]]></PicUrl>
                                <Url><![CDATA[" . $value['Url'] . "]]></Url>
                                </item> ";
            }
            $template .= "</Articles>
                                </xml>";
            echo sprintf($template, $toUser, $fromUser, $time, $msgType);

        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章