php解析http获取的json字符串变量总是空白null

来源:互联网 发布:power shell shell编程 编辑:程序博客网 时间:2024/05/16 06:40
通过http接口获取的json字符串使用json_decode始终无法正确解析,返回空白。

直接把结果字符串复制出来手动创建一个变量却正常,在前端js也能解析,搞了半天不得其解,借助强大的谷歌解决了问题,答案是接口吐出的结果包含有BOM头.

不说了,3中解决办法:

 1.

$result = @iconv("UTF-8","GBK//IGNORE",$result);    $result = @iconv("GBK","UTF-8//IGNORE",$result);    print_r(json_decode($result, true));

2.

$result = trim($result, "\xEF\xBB\xBF");print_r(json_decode($result, true));exit;
3.

if (substr($return, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {     $return = substr($return, 3); }$data = json_decode($return,true);










0 0