K
K
Khurshed Abdujalil2018-06-21 15:55:58
PHP
Khurshed Abdujalil, 2018-06-21 15:55:58

Why is it impossible to process the parsed Json?

There is a url in which data is displayed in json format

spoiler
e90b8ba08c.jpg
$ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
        curl_setopt($ch, CURLOPT_URL,$url);
        $result=curl_exec($ch);
        curl_close($ch);

json is parsed into the $result variable
spoiler
78a9eaec6f.jpg

but then json_decode returns empty value
print_r(json_decode($result))
What's wrong?
ps json_last_error() produces a syntax error, but if $result is copied and pasted into the code and decoded, then
pps works fine
var_dump($result); //string(26) "{"id":1,"bc":2,"asd":3}"
        var_dump(json_decode($result)); //NULL

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Khurshed Abdujalil, 2018-06-21
@akhur

found a solution

// This will remove unwanted characters.
for ($i = 0; $i <= 31; ++$i) { 
    $result = str_replace(chr($i), "", $result); 
}
$result = str_replace(chr(127), "", $result);

// This is the most common part
// Some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
// here we detect it and we remove it, basically it's the first 3 characters 
if (0 === strpos(bin2hex($result), 'efbbbf')) {
   $result = substr($result, 3);
}

$result = json_decode( $result );
print_r($result);

A
Anton, 2018-06-21
@Eridani

** lied
Because the result of json_decode without the second parameter is an object, not an array.
Here json_decode($res, true) will return an array

A
Alexander Aksentiev, 2018-06-21
@Sanasol

In any incomprehensible situation with json
json_last_error ()
json decode still does not throw errors itself, it seems like they want to fix it, but someday maybe never)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question