Be a Supporter!
Fallin-Again
Fallin-Again
  • Member since: Aug. 24, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Php Json & Facebook Feb. 25th, 2013 @ 02:39 PM Reply

I have been searching all day and trying many different methods. I cant seem to get this code to display any information whatsoever.

<?php
$graph = json_decode(file_get_contents("https://graph.facebook.com/122243664545681/photos"));

$picture = $graph->picture;
$pageid = $graph->id;
echo  $picture.' '.$pageid;
?>

BBS Signature
citricsquid
citricsquid
  • Member since: Jun. 25, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to Php Json & Facebook Feb. 25th, 2013 @ 02:48 PM Reply

<?php

$graph = json_decode(file_get_contents("https://graph.facebook.com/122243664545681/photos"));

foreach($graph->data as $photo) {
        print_r($photo->picture);
}

The JSON contains multiple pictures.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Php Json & Facebook Feb. 25th, 2013 @ 02:53 PM Reply

The Facebook JSON is stored inside a "data" index, which is an array, so you will need to access the 0-th element of the "data" array (or, in this case, the data object, since you aren't using the JSON as an array) to get the data you want:

<?php
$graph = json_decode(file_get_contents("https://graph.facebook.com/122243664545681/photos"));

$picture = $graph->data[0]->picture;
$pageid = $graph->data[0]->id;
echo  $picture.' '.$pageid;
?>

If you perform a var_dump on the "data" object it becomes a little more obvious how you should be accessing the JSON data:

<?php
$graph = json_decode(file_get_contents("https://graph.facebook.com/122243664545681/photos"));
var_dump($graph->data);
?>
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Php Json & Facebook Feb. 25th, 2013 @ 02:56 PM Reply

And citricsquid brings up a good point; I didn't think to mention that you can use a loop to dump out all the information, rather than just the first element.

Fallin-Again
Fallin-Again
  • Member since: Aug. 24, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to Php Json & Facebook Feb. 25th, 2013 @ 03:03 PM Reply

Ok ill try these out thanks a lot guys!


BBS Signature