Facebook API – Part 4
In the last article of Facebook API I show you more about the PHP SDK.
//get a Facebook object
$facebook = new Facebook(
'appId' => $fb_appid,
'secret' => $fb_secret
Get data from Facebook
After initialization we can request data from Facebook for the logged in user. The following line of code gets an array with all information that is available for the user. If you are not logged in, you will get an empty array:
$data = $facebook->api('/me');
FQL is a language which looks like the SQL language and is needed to get information from Facebook. With SQL like statements you can make detailed requests. The above example would look like the following with FQL:
$query = 'SELECT name, username FROM user WHERE uid = '.$user_id;
$response = $facebook->api(array('method' => 'fql.query', 'query' => $query));
For this we have to decide which columns we want to get returned. So we only want the name and the username for the logged in user. This also works for friends of the user. For all other users on Facebook this does not work, if they set the name or username not to display to public.
The last thing I want to show is a more complex example, where I get the id of the sender and the id of the recipient of an app request. These app requests can be sent from users to other users to invite them to an app.
//get apprequest (sender_uid)
$query = 'SELECT sender_uid, request_id FROM apprequest WHERE recipient_uid = '.$recipient_id.' AND app_id = '.$fb_appid;
$result = $facebook->api(array('method' => 'fql.query','query' => $query));