Php parses the json string variable obtained by http and is always blank null

Source: Internet
Author: User
This article mainly introduces the solution of php parsing the json string variables obtained by http, which is always blank and null. my personal experience is summarized. we recommend this article to you. Today, my colleague encountered a problem in the project. the json string obtained through the http interface cannot be correctly parsed using json_decode, and blank space is returned.

Directly copy the result string and manually create a variable, but it is normal. It can also be parsed in front-end js. after half a day, it cannot be solved, and the powerful Google solves the problem, the answer is that the interface output results contain a BOM header, which is a dead enemy of php.

If you don't talk about it, the solution is as follows:

The code is as follows:


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

Here we will record and share it with you, hoping to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.