Home >Backend Development >PHP Tutorial >PHP对了,可是对接到微信只回来array

PHP对了,可是对接到微信只回来array

WBOY
WBOYOriginal
2016-06-13 12:27:17978browse

PHP对了,可是对接到微信只返回array
$result = mysql_query($sql, $link); // 执行查询语句
 
$res=array();
while($row = mysql_fetch_array($result)){
    if(条件){
        $res[] = $row["title"].$row["answer"];
    }
}
return $res;
?>
用这个之后PHP是对,可是接到微信就变成单独的回复一个array
------解决思路----------------------
把数据连接成串,或编码成 json
------解决思路----------------------
return json_encode($res);
------解决思路----------------------
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
------解决思路----------------------

引用:
Quote: 引用:

在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');

 $sql = "select  * FROM `record` WHERE title like '%$keyword%'";
    $result = mysql_query($sql, $link); // 执行查询语句

            
     $res=array();
   
             while($row = mysql_fetch_array($result))
                      {
                 
                       
                  
                  $res[]=$row["title"];
                 
            
                      }
                header('Content-type:application/json;charset=utf-8');
                return json_encode($res);
               mysql_close($link);        
}                                  这次返回的值是[]

返回值为空?那么直接return "hello world";是什么结果?
------解决思路----------------------
引用:
Quote: 引用:

在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');

会不会是这个有错误?


%s


0


你把0去掉试试。
另外:要返回信息,不应该用return,而是echo
    public function responseMsg(){<br />       <br />        //get post data, May be due to the different environments<br />        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信发来的XML数据<br /><br />        //extract post data<br />        if(!empty($postStr)){<br />                <br />            //解析post来的XML为一个对象$postObj<br />            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);<br />        <br />            $fromUsername = $postObj->FromUserName; //请求消息的用户<br />            $toUsername = $postObj->ToUserName; //"我"的公众号id<br />            $keyword = trim($postObj->Content); //用户发送的消息内容<br />            $time = time(); //时间戳<br />            $msgtype = 'text'; //消息类型:文本<br />            $textTpl = "<xml><br />                        <ToUserName><![CDATA[%s]]></ToUserName><br />                        <FromUserName><![CDATA[%s]]></FromUserName><br />                        <CreateTime>%s</CreateTime><br />                        <MsgType><![CDATA[%s]]></MsgType><br />                        <Content><![CDATA[%s]]></Content><br />                        </xml>";<br />                $contentStr = "输入-h查看帮助吧(=?ω?=)";<br />                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgtype, $contentStr);<br />                echo $resultStr;<br />
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn