PHP no data (NULL) solution when reading JSON, phpjson_php tutorial

Source: Internet
Author: User

PHP no data (NULL) solution when reading JSON, Phpjson


When using PHP to invoke some JSON interface files, if you use file_get_contents to get the page JSON data

After using Json_decode (), the data cannot be output properly. The return value is null

This is due to PHP file_get_contents obtained data in front of three invisible BOM characters, the PHP transcoding or set the head code to no BOM still cannot solve

One possible approach is to:


  
    $strfile_get_contents// get page address $strsubstr($ STR// due to PHP problem file_get_contents The data is preceded by three invisible BOM characters using the SUBSTR function to extract the contents of the third character $json = Json_decode ($strtrue); // parsing JSON code returned as an array value Echo $json [' MOTD ']; // output An array of MOTD in JSON with an array

Another type of:

 
  PHP$strfile_get_contents// get page address $json = Json_decode (trim( $str,chr(239). CHR (187). CHR (191)),true//chr (239). chr (187). CHR (191) The BOM header that formed the UTF8 file at output, and then removed it with the trim function Print_r($json// output JSON with array

http://www.bkjia.com/PHPjc/1140692.html www.bkjia.com true http://www.bkjia.com/PHPjc/1140692.html techarticle PHP does not have data (empty) solution when reading JSON, Phpjson when using PHP to call some JSON interface files if you use file_get_contents to get the page JSON data and then use the Json_decode ( ...

  • 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.