views:

209

answers:

5
<?php
$handle = fopen("https://graph.facebook.com/[email protected]&amp;type=user&amp;access_token=2227472222|2.mLWDqcUsekDYK_FQQXYnHw__.3600.1279803900-100001310000000|YxS1eGhjx2rpNYzzzzzzzLrfb5hMc.", "rb");
$json = stream_get_contents($handle);
fclose($handle);
echo $json;
$obj = json_decode($json);
print $obj->{'id'};
?>

Here is the JSON: {"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}

It echos the JSON but I was unable to print the id.

Also I tried:

<?php
$obj = json_decode($json);
$obj = $obj->{'data'};
print $obj->{'id'};
?>
+3  A: 

It looks like the key "data" is an array of objects, so this should work:

$obj = json_decode($json);
echo $obj->data[0]->name;
Tom Haigh
+2  A: 

Have you tried $obj->data or $obj->id?

Update: Others have noted that it should be $obj->data[0]->id and so on.

PS You may not want to include your private Facebook access tokens on a public website like SO...

pr1001
+1 for privacy warning.
yc
+3  A: 

Note that there is an array in the JSON.

{
    "data": [   // <--
      {
        "name": "Sinan \u00d6zcan",
        "id":   "610914868"
      }
    ]           // <--
}

You could try $obj = $obj->{'data'}[0] to get the first element in that array.

KennyTM
+1  A: 

data is an array, so it should be:

print $obj[0]->{'id'};
Felix Kling
+1  A: 

It's a bit more complicated than that, when you get an associative array out of it:

$json = json_decode('{"data":[{"name":"Sinan \u00d6zcan","id":"610914868"}]}', true);

Then you may echo the id with:

var_dump($json['data'][0]['id']);

Without assoc, it has to be:

var_dump($json->data[0]->id);
cypher