收藏
回答

公众号关键词回复出现bug!

微信公众号本来简单的开发,但是今天我遇到这个问题很奇怪,下面我将贴出我的代码及错误:


关键词回复报错提示:该公众号提供的服务出现故障,请稍后再试。

而我公众号开发里面配置是能够连通的,如下图:




这说明我的程序是能够接入微信的,另外,我程序代码里打印log,也是可以打印出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);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $this->postObj = $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>";
            if($postObj->MsgType=='event'){
                switch ($postObj->Event){
                    case 'CLICK':
                     
                    break;
                    case 'subscribe':
                    //获取用户信息,并存入数据库
                    $openid = $fromUsername;
                    //查询是否已有账号
                    $islive = syDB('member')->find(array('openid'=>$openid));
                    if(!$islive){
                        $access_token = $this->getAccessToken();
                        $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
                        $user = file_get_contents($url);
                        $user = json_decode($user,true);
                        //$this->file_url = $user['headimgurl'];
                         
                        $n=syDB('member')->create(array('user'=>$user['nickname'],'pass'=>'','email'=>'','regtime'=>$time));
                        if($n){
                             
                            syDB('member_field')->create(array('aid'=>$n,'sex'=>$user['sex'],'openid'=>$user['openid'],'touxiang'=>$user['headimgurl']));
                        }
                         
                         
                    }
                    break;
                     
                     
                }
                 
                 
                 
                 
            }else{
                //write_log($keyword);
                if($keyword){
                    $data = syDB('keywords')->find(" keywords like '%".$keyword."%' and isshow=1");
                    if($data){
                        if($data['body']!=''){
                            $contentStr=$data['body'];
                        }else{
                            $contentStr="不知道你说什么,能否换一句?";
                        }
                    }else{
                        $contentStr="不知道你说什么,能否换一句?";
                    }
                     
                    $msgType = "text";
                    $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                    //write_log($resultStr);
                    echo $resultStr;
                    exit;
                }
             
            }
             
             
 
        }else {
            echo "";
            exit;
        }
    }

最后的  write_log($resultStr);  能够打印如下信息:

'<xml>
                                <ToUserName><![CDATA[ob5HO54Nkpt-HZm2kxrfh3nazVVU]]></ToUserName>
                                <FromUserName><![CDATA[gh_05c06c82d54f]]></FromUserName>
                                <CreateTime>1554911946</CreateTime>
                                <MsgType><![CDATA[text]]></MsgType>
                                <Content><![CDATA[不知道你说什么,能否换一句?]]></Content>
                                <FuncFlag>0</FuncFlag>
                                </xml>'


我搞了一晚上没有解决这是什么原因。。。请求社区大神给我解答一下。。。谢谢。。。

回答关注问题邀请回答
收藏

2 个回答

  • 疯狂的小辣椒
    疯狂的小辣椒
    2019-04-11

    你好,若遇到以下情况,系统会提示“该公众号暂时无法提供服务,请稍后再试”: 
    1、开发者在5秒内未回复任何内容

     2、开发者回复了异常数据,比如JSON数据等 

    建议可参考文档(被动回复用户消息文档如下: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140543 )排查是否存在以上问题。


    2019-04-11
    有用
    回复 1
    • 如沐春
      如沐春
      2019-04-11

      我程序执行到最后输出XML,没有json数据和其他数据。

      '<xml>
                                    <ToUserName><![CDATA[ob5HO54Nkpt-HZm2kxrfh3nazVVU]]></ToUserName>
                                    <FromUserName><![CDATA[gh_05c06c82d54f]]></FromUserName>
                                    <CreateTime>1554955724</CreateTime>
                                    <MsgType><![CDATA[text]]></MsgType>
                                    <Content><![CDATA[不知道你说什么,能否换一句?]]></Content>
                                  </xml>'

      实在搞不懂。。。


      2019-04-11
      回复
  • 祺爸💎
    祺爸💎
    2019-04-11

    这个PHP代码真是惨不忍睹啊

    2019-04-11
    有用
    回复 1
    • 如沐春
      如沐春
      2019-04-11

      简单开发,没有进行封装,胜在方便快捷咯

      2019-04-11
      回复
登录 后发表内容