Why has json decode started returning null #2

Reading the previous post

below my solution (if someone is interested)

Create the function:

// see https://www.the-art-of-web.com/php/http-get-contents/
function http_get_contents($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($ch, CURLOPT_ENCODING ,"");
    curl_setopt($ch, CURLOPT_ENCODING , "gzip");   
    if (FALSE === ($retval = curl_exec($ch))) {
        echo curl_error($ch);
    } else {
        return $retval;
    }
}
//////
then:

$live_data_url = ' your http or https address';
$live_data = preg_match("/^http/", $live_data_url) ? http_get_contents($live_data_url) : file_get_contents($live_data_url);
// important ! 
//Take a look here
// https://stackoverflow.com/questions/17219916/json-decode-//returns-json-error-syntax-but-online-formatter-says-the-json-is-ok
//
if (0 === strpos(bin2hex($live_data), 'efbbbf')) {
   $live_data = substr($live_data, 3);
}
$live_data       = json_decode($live_data, true);
$live_data       =  $live_data[0]; // if data are in  position 0 inside the array
// take a look at your demounted json file
echo '<pre>';
print_r($live_data);
echo '</pre>'; 
// proceed to extract your data
$data1              = $live_data['data1'];
$data2              = $live_data['data2'];
//etc etc

//

Well, bye bye everybody

Have you checked if the JSON file still exists in the location you are trying to pull it from? Have you checked what value the file_get_contents returned? Do you have error reporting on?

Wild guess is that the file doesn’t exists anymore and file_get_contents returns then false. And when you run false through json_decode it returns null. This should give you a warning tho.

You can also check last json error with http://php.net/manual/en/function.json-last-error.php after running json_decode, but this is pointless if the file_get_contents doesn’t return anything.

Have you checked if the JSON file still exists in the location you are trying to pull it from?
YES

$handle_live = fopen($live_data_url,'r');
 if($handle_live === false){
    $handle_live = 0;                                                                           
    fclose($handle_live);
 } else {  
$live_data = preg_match("/^http/", $live_data_url) ? http_get_contents($live_data_url) : file_get_contents($live_data_url);
…


Do you have error reporting on?
YES
using the follow function

function json_validate($string)
{
    // decode the JSON data
    $result = json_decode($string);

    // switch and check possible JSON errors
    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            $error = ''; // JSON is valid // No error has occurred
            break;
        case JSON_ERROR_DEPTH:
            $error = 'The maximum stack depth has been exceeded.';
            break;
        case JSON_ERROR_STATE_MISMATCH:
            $error = 'Invalid or malformed JSON.';
            break;
        case JSON_ERROR_CTRL_CHAR:
            $error = 'Control character error, possibly incorrectly encoded.';
            break;
        case JSON_ERROR_SYNTAX:
            $error = 'Syntax error, malformed JSON.';
            break;
        // PHP >= 5.3.3
        case JSON_ERROR_UTF8:
            $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
            break;
        // PHP >= 5.5.0
        case JSON_ERROR_RECURSION:
            $error = 'One or more recursive references in the value to be encoded.';
            break;
        // PHP >= 5.5.0
        case JSON_ERROR_INF_OR_NAN:
            $error = 'One or more NAN or INF values in the value to be encoded.';
            break;
        case JSON_ERROR_UNSUPPORTED_TYPE:
            $error = 'A value of a type that cannot be encoded was given.';
            break;
        default:
            $error = 'Unknown JSON error occured.';
            break;
    }

    if ($error !== '') {
        // throw the Exception or exit // or whatever :)
        exit($error);
    }

    // everything is OK
    return $result;
}

//

<off-topic>
@stefano13 when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.
</off-topic>

Have you checked what value the file_get_contents returned? ← and this? The function returns false on failure. So if that return value is false the file couldn’t be read for some reason. Maybe file permissions have changed?

Sorry but I do not understand your question.

Adding
var_dump($live_data);

after

$live_data = preg_match("/^http/", $live_data_url) ? http_get_contents($live_data_url) : file_get_contents($live_data_url);

I check returned json file data:

Consider that adding:

 $output = json_validate($live_data);
 print_r($output);

after

if (0 === strpos(bin2hex($live_data), 'efbbbf')) {
   $live_data = substr($live_data, 3);
}

the function json_validate($live_data) returns the error type in case of wrong json or json contents if json file is well formed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.