ホームページ >バックエンド開発 >PHPチュートリアル >PHP は正しいですが、WeChat に接続すると配列のみが返されます。
133986e0c68d68155c232c71b6210b08
これを使用すると、PHP は正しいですが、WeChat を受信すると、別の返信配列になります
データを文字列に連結するか、json にエンコードします
return json_encode($ res) ;
return json_encode($res);
誰か知っていますかそれをマスターするには?
echo json_encode の前に、 header('Content-type:application/json;charset=utf-8');
echo json_encode の前に追加します。 : header( 'Content-type:application/json;charset=utf-8');
会わないのはこれですか?b2a0af5a8fd26276da50279a1c63a57a
ea5d8177d19f22584533e5c37c389942d3242fa0f72a59f12bbb2807edba61b76671a89dce89e879d9e9c6d81d03862b
c5123754d1f4829fae4905e8abb602f9d3242fa0f72a59f12bbb2807edba61b742a4b8d57eb0afadcf16b7a02c69caaf
246311df1688542638dc52b54a1a4c87%se660f1169ff44ea75c5a982fcb1cde61
在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);
} 次の戻り値は[]
在echo json_encode之前,加上:header('Content-type:application/json;charset=utf-8');
你的表字段是UTF-8,所以应该 mysql_query("set names utf8",$link);
另外,有个地方稍微改下(改不改都行,不过echo的数据都应该是XML格式的):
if(!empty($keyword )){ $msgType = "text"; $contentStr = chaxun($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}else{ $msgType = "text"; $contentStr = "input something..."; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}
你的表字段是UTF-8,所以应该 mysql_query("set names utf8",$link);
另外,有个地方稍微改下(改不改都行,不过echo的数据都应该是XML格式的):
if(!empty($keyword )){ $msgType = "text"; $contentStr = chaxun($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}else{ $msgType = "text"; $contentStr = "input something..."; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr;}
1、你是在做微信应用,而微信都是 utf-8 编码的
所以你 mysql_query("set names GBK",$link); 是不对的
要 mysql_query("set names utf8",$link);
2、你把传入的 utf-8 数据 $keyword 当做 gbk 解释
这就造成了 chaxun 函数返回空数组
当然确实不存在的时候,也是返回空数组的
3、由
$contentStr= chaxun($keyword);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
可知,$contentStr 应是一个字符串,而 chaxun 返回的是数组,所以可能需要
$contentStr = json_encode(chaxun($keyword));
或
$contentStr = join(',', chaxun($keyword));
1、用echo 回复公众号暂停服务====》可能是代码有语法错误或者error,在你的域名里运行这个脚本看看有没有报错。
2、试一下,不进行查找数据库,直接让$contentStr = "字符串";然后echo $resultStr; 看看是不是数据取出问题
1、用echo 回复公众号暂停服务====》可能是代码有语法错误或者error,在你的域名里运行这个脚本看看有没有报错。
2、试一下,不进行查找数据库,直接让$contentStr = "字符串";然后echo $resultStr; 看看是不是数据取出问题
1、你是在做微信应用,而微信都是 utf-8 编码的
所以你 mysql_query("set names GBK",$link); 是不对的
要 mysql_query("set names utf8",$link);
2、你把传入的 utf-8 数据 $keyword 当做 gbk 解释
这就造成了 chaxun 函数返回空数组
当然确实不存在的时候,也是返回空数组的
3、由
$contentStr= chaxun($keyword);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
可知,$contentStr 应是一个字符串,而 chaxun 返回的是数组,所以可能需要
$contentStr = json_encode(chaxun($keyword));
或
$contentStr = join(',', chaxun($keyword));