Home  >  Article  >  Backend Development  >  php解析http获取的json字符串变量总是空白null_php技巧

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

WBOY
WBOYOriginal
2016-05-16 20:22:191022browse

今天同事项目中遇到一个问题,通过http接口获取的json字符串使用json_decode始终无法正确解析,返回空白。

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

不说了,直接上解决办法:

复制代码 代码如下:

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

这里记录一下,分享给大家,希望对大家能够有所帮助。

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