json_decode returns NULL after webservice call

Viewed 114542

There is a strange behaviour with json_encode and json_decode and I can't find a solution:

My php application calls a php web service. The webservice returns json that looks like this:

var_dump($foo):
string(62) "{"action":"set","user":"123123123123","status":"OK"}"

now I like to decode the json in my application:

$data = json_decode($foo, true)

but it returns NULL:

var_dump($data):
NULL

I use php5. The Content-Type of the response from the webservice: "text/html; charset=utf-8" (also tried to use "application/json; charset=utf-8")

What could be the reason?

24 Answers

Well, i had a similar issue and the problems was the PHP magic quotes in the server... here is my solution:

if(get_magic_quotes_gpc()){
  $param = stripslashes($_POST['param']);
}else{
  $param = $_POST['param'];
}
$param = json_decode($param,true);

EDIT: Just did some quick inspection of the string provided by the OP. The small "character" in front of the curly brace is a UTF-8 B(yte) O(rder) M(ark) 0xEF 0xBB 0xBF. I don't know why this byte sequence is displayed as  here.

Essentially the system you aquire the data from sends it encoded in UTF-8 with a BOM preceding the data. You should remove the first three bytes from the string before you throw it into json_decode() (a substr($string, 3) will do).

string(62) "{"action":"set","user":"123123123123","status":"OK"}"
            ^
            |
            This is the UTF-8 BOM

As Kuroki Kaze discovered, this character surely is the reason why json_decode fails. The string in its given form is not correctly a JSON formated structure (see RFC 4627)

Try this

$foo = utf8_encode($foo);
$data = json_decode($foo, true);
"{"action":"set","user":"123123123123","status":"OK"}"

This little apostrophe in the beginning - what is it? First symbol after the doublequote.

Before applying PHP related solutions, validate your JSON format. That may be the problem. Try below online JSON format validator. If your JSON format is invalid, correct it first, because PHP doesn't decode invalid JSON strings.

https://jsonformatter.org/

Laravel specific answer: I got the same issue in Laravel. And this did the trick for me

$result = json_decode($result->getContent(), true);

In my case, when I was printing to the screen, json was fine and I copied and decode with json_deocode() function. It was working fine. But, when I was trying to put jsonString directly in the function, it was returning null because quotes were coming like these ". So I used htmlspecialchars_decode() function and now it is working fine. I am new here, so if I am making any mistakes in writing answer then sorry for that. I hope it'll help somebody.

Sometimes the problem is generated when the content is compressed, so adding the Accept-Encoding: identity header can solve the problem without having to wrangle with the response.

   $opts = array(
        'http' =>
            array(                    
                'header'  =>
                    array(
                        'Accept-Encoding: identity',
                    ),
            ),           
    );

    $context = stream_context_create($opts);

    $contents = file_get_contents('URL', false, $context);
Related