Home > Backend Development > PHP Tutorial > php解析http获取的json字符串变量总是空白null_PHP

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

WBOY
Release: 2016-05-31 13:16:37
Original
835 people have browsed it

今天同事项目中遇到一个问题,通过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);

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template