如何转换facebook图形JSON输出?

Not sure if I'm actually doing this right but got a small piece of code from a tutorial I was watching on youtube, unfortunately the guy never posted part 2 or 3 so was left with just what he had written on the first video.

I've been trying to access all the PUBLIC posts that are available and have seen an easy way of accessing them via a link: https://graph.facebook.com/search?q=i passed my exams&type=post&locale=en_GB

it presents what seems to be a massive array of information but how can I process this apparent JSON output using PHP into understandable information ? I've seen it done on a similar site thats making waves across the internet and wanted to try doing it myself.

Is this possible using the following code:

$fbquery = "https://graph.facebook.com/search?q=i hate my boss&type=post";
$fb = file_get_contents($fbquery, 'rb');
$fbarray = json_decode($fb, true);
echo $fbarray['message'];

or am I going wrong with that somewhere? please bare in mind that its public information available and using the link in any address bar will produce and output without having the user login or using an access token.

Thanks for any help and its appreciated!

Dave.

After json_decode you get an object.

If you do $fbarray->data you will get an array of objects. Loop each object and check for its type.

Like

for(key in $fbarray->data){
  $element = $fbarray->data[key]; // you get each element.
  if($element->type =="photo") // check for different types
  {
     // display as photo.
    echo '<img src="$element->picture">';
  }
  if($element->type =="status") 
  {
     // display message.
    echo $element->message;
  }
}

You can use an online json editor like jsonedtor to get a tree view. This helps to understand tree structure of the json.

You've got a couple of problems here. 1) You should to urlencode your search string. Second, $fbarray is an object. You won't get just one item. Try this code:

$str = "i hate my boss";
$str = urlencode($str);
$fbquery = "https://graph.facebook.com/search?q={$str}&type=post";
$fb = file_get_contents($fbquery, 'rb');
$fbarray = json_decode($fb, true);
foreach ($fbarray as $item) { 
   echo $item['message'];
}

To test a public query like this, you can paste your $fbquery url into the address bar of a browser and see if you get data returned.