Home >Backend Development >PHP Tutorial >The json string variable obtained by php parsing http is always blank null, jsonnull_PHP tutorial
I encountered a problem in a colleague’s project today. The json string obtained through the http interface always uses json_decode Unable to parse correctly, returns 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:
Copy code 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.