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.
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
Post a Comment