Facebook has some rather convenient out-of-the box tools for interacting with its back end. At the core of the platform is a set of API calls that wrap more complex code into a single call. In fact, most of the API calls are simply wrappers for complex FQL calls. So, without further adieu, let’s take a look at what you have available to you through the API calls.

Facebook’s API methods are broken into logical groups of calls. These API calls are also where your first interaction with the platform comes into play. You don’t need to use the client library—it just makes things faster since you would need to write these in your language in order to interact with the platform.

To set up the client library for PHP, you simply include the client library in your code. For example, the following snippet makes sure users are logged on to Facebook before displaying a list of the user’s friends. Below Figure shows the results.
<?php
'; $facebook_config['secret_key'] = '';/** * include the Facebook client library */ require_once('/**
* Set the configuration settings for Facebook
*/
$facebook_config['debug'] = false;
$facebook_config['api_key'] = '<your_api_key>';
$facebook_config['secret_key'] = '<your_secret_key>';
/**
* include the Facebook client library
*/
require_once('<path_to_client_library>/facebook.php');
/**
* Set up the facebook object
*/
$facebook = new Facebook($facebook_config['api_key'], $facebook_config['secret']);
/**
* Ensure the user has logged on to Facebook
*/
$user = $facebook->require_login();
/**
* Make an API call to call get a user's friends using the PHP library's
* library
*/
$friends = $facebook->api_client->friends_get();
echo "<pre>Friends:" . print_r($friends, true). "</pre>";
?>

You’ll notice a few things in this example. First, you’re just throwing the results from the friends object on the screen. I’ll cover interacting with the resulting code with FBML a bit later. Second, the syntax for interacting with the API is abstracted into methods in the Facebook object. You first set up the Facebook object with the API and secret keys that Facebook assigns to you when you set up your application.


You’ll also notice that you require the user to be logged on to Facebook in order to execute the code. This example uses the require_login() method and stores the user’s identification number (UID) in the variable user. The next call actually queries the Facebook API method friends.get by wrapping it in the PHP method friends_get. Since the facebook object holds the user’s identification number (their primary key or UID), there’s no need to pass the UID to the API request. From this short sample code, you can see that the PHP code is actually setting up a REST client to call the facebook.friends.get API method and returns an array of user identifiers:

/**

* Returns the friends of the current session user.

* @return array of friends

*/

public function friends_get() {

if (isset($this->friends_list)) {

return $this->friends_list;

}

return $this->call_method('facebook.friends.get', array());

}
The client libraries allow you to concentrate on developing your application rather than recoding your interactions with the platform. Since you need to know what you can do with the API.

0 comments