Is the JSON object NULL when the data from the url is small?

I have decoded a JSON object using url, for example:

    $var = json_decode(file_get_contents($url), true);

The data I receive is the new battle API, which is returned as a JSON object containing character data.

  • Here is the actual link in which the symbol is found, and there is data that should be generated: Character found .
  • Now here is the link for the character that does not exist and is generated, and the error: The character was not found .

Errors are returned as JSON objects that contain the status and reason attributes. The value of the status attribute will always be nok.

My problem is that the character is NOT FOUND, the JSON $ var object is NULL, where if it is FOUND, the JSON $ var object contains the correct data. I need to check if the status is "nok", so I can display the corresponding error message

I have:

  • Check the links to make sure they are generated correctly.
  • Changed json_decode(file_get_contents($url), true)to: json_decode(file_get_contents($url), false)and tried to access $ var as an object.
  • Tried to use cURL instead file_get_contents($url)

I posted the Battle.net APIs on the forums, but I thought I would try it too.

+3
source share
1 answer

This is because the page with the symbol was not found, it returns an 404 Not FoundHTTP response code , which makes a lot of sense, but is considered an error.

, file_get_contents, false (404, 500 ..) HTTP-.

, json_decode , :

$context = stream_context_create(array(
    'http' => array('ignore_errors' => true),
));

$var = json_decode(file_get_contents($url, false, $context), true);
+3

All Articles