Parsing Facebook Social Graph’s JSON Output with PHP

Facebook’s core is the social graph: people and the connections.  And the concept of Facebook Graph API can be simplified as: objects and connections.

As mentioned in previous post. In Facebook Graph API, every object has a unique ID. Facebook Graph API provides an easy way accessing Facebook objects:

https://graph.facebook.com/ID

For example:

https://graph.facebook.com/MyGreatNameHosting

The output of the above URL will be below:

{
   "id": "121863104593095",
   "name": "Affordable Web Hosting and Domain",
   "picture": "http://profile.ak.fbcdn.net...
   "link": "http://www.facebook.com/MyGreat..",
   "likes": 4,
   "cover": {
      "cover_id": 255448074567930,
      "source": "http://a2.sphotos.ak.fbcdn...",
      "offset_y": 0
   },
   "category": "Website",
   "is_published": true,
   "website": "http://www.mygreatname.com",
   "username": "MyGreatNameHosting",
   "company_overview": "We have hundreds of....",
   "about": "Affordable web hosting and d.....",
   "talking_about_count": 125
}

As you can see, this is rather easy to get the information of a Facebook object. The next question is how to extract them so that useful information can be used on the Facebook App.

The output return above is in JASON format. There are many methods to parse the data. Today I am going to use PHP to parse the JASON data.

Parsing Facebook Social Graph’s JSON Output with PHP

 <?php
// Path of social graph
$fb_graph =’https://graph.facebook.com/mygreatnamehosting’;

// Get the file contents in json file format
$fb_graph_json = file_get_contents($fb_graph);
?>

Now we get the file in jason file format. If we print out the $fb_graph_json (echo $fb_graph_json;), it will look like:

{“id”:”121863104593095″,”name”:”Affordable Web Hosting and Domain Name”,”picture”:”http…..

The next step is to decode the JSON string into an array so that we can get the data easily:

<?php
// Path of social graph
$fb_graph =’https://graph.facebook.com/mygreatnamehosting’;

// Get the file contents in json file format
$fb_graph_json = file_get_contents($fb_graph);

// Use json_decode to decode the JSON string into an array
$fb_graph_json_array = json_decode($fb_graph_json, true);
?>

If we print out the $fb_graph_json_array with the following lines:

echo ‘<pre>’;
  print_r($fb_graph_json_array);
echo ‘</pre>’;

We will get our familiar array format, as shown below:

Once we get the Facebook social graph into an array, this is rather easy to fetch the data with the corresponding key. Let’s play around.

Loop Through the Whole Array

// Loop through the array
foreach ($fb_graph_json_array as $key => $value) {
   echo $key, ' : ', $value . "
";
}

The output on the screen will be:

Fetch Individual Value From the Array

Let’s get the “id” and “number of likes” of this user:

// Get the value with corresponding key
echo "The id of this user is: " . $fb_graph_json_array['id'] ."
";
echo "Total number of likes : " . $fb_graph_json_array['likes'] ."
";

The output on the screen will be:

Therefore this is rather easy to integrate the object information into Facebook Apps.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>