How Facebook Graph API Access Objects

The Facebook SDK for PHP Development Environment has been set up. We already know the basic file structure of a Facebook App. It’s time to see how to use Facebook Graph API to access information of objects from Facebook servers.

Let’s see how to get the basic user information of the App user. Type the following codes just below the <body> tag:

<?php
      $user = $facebook->api('/me');
      print_r($user);
?>

Upload the files to hosting account and access the Facebook App again. The above codes should produce the output as below:

How the Codes Works

As I mentioned before in my post – Facebook Graph API Introduction that Facebook Graph API provides an easy way accessing Facebook objects:

https://graph.facebook.com/objectID

Therefore we can use the above URL to query the user object of the Graph API.

$user = $facebook->api(argument);

This line of code simply make a call to the Graph API with the api() function of the Facebook class. This is equal sending a GET request to https://graph.facebook.com/(argument) to get the
required information. Actually the above line of codes can be written as:

https://graph.facebook.com/(argument)

Look at the codes again:

$user = $facebook->api(‘/me’);

This is just sending a GET request with the following URL:

 https://graph.facebook.com/me

The codes can be illustrated with the diagram below:

The $user variable will store all the information with the api() function. Therefore we can print out the $user information with the following line of code:

print_r($user);

Actually the code returned a JSON object. However the api() function will convert the JSON object into a PHP array which is much easier to manipulate.

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>