search

Home  >  Q&A  >  body text

php - file_get_contents($url); input json

Use file_get_contents($url); to return json. It cannot be parsed using json_decode. What should I do? I have also used the curl method, but it does not work

为情所困为情所困2755 days ago955

reply all(10)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 13:00:39

    This problem has been solved by myself. The data returned by the third party is ascll, so it needs to be converted into utf-8 format. It has nothing to do with json_decode

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:00:39

    You need to verify whether the format is correct, do not upload the code on BB:

    <?php
    function treatJsonString($string)
    {
        $jsonData = json_decode($string, true);
    
        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                return $jsonData;
                break;
            case JSON_ERROR_DEPTH:
                print '[Error] - Maximum stack depth exceeded' . PHP_EOL;
                break;
            case JSON_ERROR_STATE_MISMATCH:
                print '[Error] - Underflow or the modes mismatch' . PHP_EOL;
                break;
            case JSON_ERROR_CTRL_CHAR:
                print '[Error] - Unexpected control character found' . PHP_EOL;
                break;
            case JSON_ERROR_SYNTAX:
                print '[Error] - Syntax error, malformed JSON' . PHP_EOL;
                break;
            case JSON_ERROR_UTF8:
                print '[Error] - Malformed UTF-8 characters, possibly incorrectly encoded' . PHP_EOL;
                break;
            default:
                print '[Error] - Unknown error' . PHP_EOL;
                break;
        }
        return null;
    }
    
    
    $jsonString = '{"x":123,"s":[{"a":"1"}]';
    
    var_dump(treatJsonString($jsonString));
    

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:00:39

    First check whether your json is in normal json format
    Then check whether your php file is utf-8 without BOM
    I have encountered similar problems before, and it will be fine after removing the BOM~

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:00:39

    Send the returned data and take a look

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-16 13:00:39

    No problem, I analyzed it:

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-16 13:00:39

    json_decode($json, true)
    

    With true, it means it will be parsed into an array of php

    reply
    0
  • PHPz

    PHPz2017-05-16 13:00:39

    Confirm first. Is the returned thing json?

    reply
    0
  • 为情所困

    为情所困2017-05-16 13:00:39

    First, make sure your Json is escaped in other ways. If not, you can use the Json formatting verification tool to check if there is a problem.

    Online Json format verification tool
    http://www.bejson.com/

    reply
    0
  • 習慣沉默

    習慣沉默2017-05-16 13:00:39

    No problem, I suggest you check it carefully

    reply
    0
  • 黄舟

    黄舟2017-05-16 13:00:39

    Double check whether the JSON data format is correct

    reply
    0
  • Cancelreply