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:
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:
Look at the codes again:
$user = $facebook->api(‘/me’);
This is just sending a GET request with the following URL:
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:
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.