2

I am new to JSON and having a problem with checking at getting the error message when there is an error. My code works fine when the result is not an error, so I do somewhat understand what I am doing.
This is the error JSON that I am trying to parse:

{
   "error": {
      "message": "Unsupported get request.",
      "type": "GraphMethodException",
      "code": 100
   }
}

Here is my code that fails:

$jsonurl = "http://graph.facebook.com/JubilationDanceMinistry";
//valid $jsonurl = "http://graph.facebook.com/WhitworthACM";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);

var_dump($json_output);
// This returns NULL


if (property_exists($json_output->error)) {
        echo "<p>error: $json_output->error->{'message'} </p>";
    } else {
        echo "<p>no error :(</p>";
}

$facebook_id = $json_output->{'id'};
$facebook_name = $json_output->{'name'};
$facebook_link = $json_output->{'link'};
2
  • Why define 2nd (invalid) and 3rd (default) params? Commented Nov 17, 2012 at 5:34
  • I'm not quite sure. I am a novice with JSON. This was just code I found to parse JSON from a URL and it works with the valid URL. Would you advise me to change it to something else? Commented Nov 17, 2012 at 5:47

2 Answers 2

1

Because the url returns the 400 Bad Request.

By default, you can't use file_get_contents function to get the response content when the http status code is 400.

You need to set ignore_errors options to true.

$opts = array(
  'http'=>array(
    'ignore_errors' => true
  )
);
$context = stream_context_create($opts);
$jsonurl = "http://graph.facebook.com/JubilationDanceMinistry";
$json = file_get_contents($jsonurl, false, $context);
var_dump($json);
Sign up to request clarification or add additional context in comments.

6 Comments

It is not an invalid URL however. It is related to facebook.com/JubilationDanceMinistry (error is based on privacy) You can visit graph.facebook.com/JubilationDanceMinistry
@michaellindahl Ok, no matter the url is, the problem is the http response status, you need to get the error message, right?
Not exactly. There is no http error as there is going to always be a JSON object. I want to get the "Unsupported get request." from the JSON at graph.facebook.com/JubilationDanceMinistry. I want if in the JSON the element error is present, echo the message.
@michaellindahl Have you tried my code? It gives what you want.
@michaellindahl The status code is set by the server side of facebook, facebook just think the way you call the url is a bad request. It's nothing related of processing the JSON, you need to get the json content first before you decode it.
|
0

You can't chain multiple ->'s with string interpolation.

You'll have resort to passing multiple arguments to echo or to string concatenation:

    echo "<p>error: ", $json_output->error->{'message'}, " </p>";

1 Comment

The error happens before the code even reaches this point. The error happens before line 5 when it returns NULL. However, is this code better: $json_error = $json_output->error; $json_message = $json_error->message; echo "<p>JSON message: ".$json_message.'</p>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.