Home > php教程 > php手册 > php解析http获取的json字符串变量总是空白null

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

WBOY
Release: 2016-06-13 09:13:42
Original
849 people have browsed it

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

 这篇文章主要介绍了php解析http获取的json字符串变量总是空白null的解决方法,个人经验的总结,这里推荐给大家。

 

 

今天同事项目中遇到一个问题,通过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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template