Home >Backend Development >PHP Tutorial >The json string variable obtained by php parsing http is always blank null_PHP tutorial

The json string variable obtained by php parsing http is always blank null_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:05:42781browse

The json string variable obtained by php parsing http is always blank and null

This article mainly introduces the solution to the problem that the json string variable obtained by php parsing http is always blank and null. , a summary of personal experience, recommended here to everyone.

Today, a colleague encountered a problem in his project. The json string obtained through the http interface cannot be parsed correctly using json_decode and returns a blank.

Copy the result string directly and create a variable manually, but it works fine. It can also be parsed in the front-end js. I couldn’t figure it out for a long time. I solved the problem with the help of the powerful Google. The answer is that the result spit out by the interface contains a BOM header. The BOM header can be said to be the mortal enemy of PHP

No more talking, let’s go straight to the solution:

The code is as follows:


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

Record it here and share it with everyone. I hope it can be helpful to everyone.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/962931.htmlTechArticleThe json string variable obtained by php parsing http is always blank null. This article mainly introduces the json string variable obtained by php parsing http. The solution to the problem that json string variables are always blank and null, a summary of personal experience,...
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