收藏
回答

关注回复代码在linux上可以,但是在window上不行

明明是同一个代码,但是在linux上可以运行消息被动回复,但是在window上却不可以。但是却可以获取access_token。凡事是消息回复的都不行,但是其他请求接口确是可以。我到底是哪弄错了(我用的是tp5框架)

class Debug extends Base
{
     public function index()
     {
         if (isset($_GET['echostr'])) {
             $this->valid();
         }else{
             $this->responseMsg();
         }
     }
 
    /**
     *微信入接入
     */
    public function valid()
    {
        $echoStr = $_GET["echostr"];
 
        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
 
    /**
     * 判断服务器是否可以接入
     * @return bool
     */
    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);
            //发送消息方ID
            $fromUsername = $postObj->FromUserName;
            //接收消息方ID
            $toUsername = $postObj->ToUserName;
            //消息类型
            $form_MsgType = $postObj->MsgType;
            $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>";
            //事件消息
            if($form_MsgType=="event")
            {
                //获取事件类型
                $form_Event = $postObj->Event;
                //订阅事件
                if($form_Event=="subscribe")
                {
                    //回复欢迎文字消息
                    $msgType = "text";
                    $contentStr = "感谢您关注Super King的微信公众号[玫瑰]";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, time(), $msgType, $contentStr);
                    echo $resultStr;
                    exit;
                }
            }
            //如果用户发送内容不为空,回复“谢谢您的回复!”
            if(!empty( $keyword ))
            {
                $msgType = "text";
                $contentStr = "谢谢您的回复!";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }else{
                echo "Input something...";
            }
             
        }else {
            echo "";
            exit;
        }
    }
}



最后一次编辑于  2018-09-13  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏关注问题回答

1 个回答

  • 昂扬
    昂扬
    2018-10-22

    请问Linux下怎么接收微信回复的事件消息啊?

    2018-10-22
    赞同
    回复