Facebook Cookbook - Jay Goldman [133]
$facebook = new Facebook($api_key, $secret);
$facebook->require_frame();
$user = $facebook->require_login();
The first line instantiates a new instance of the Facebook Client Library, which we’ll use to make all of our subsequent calls. The second checks to see whether we’re inside of a frame (e.g., an iFrame), and then forces us to the login page to make sure that this session is valid. The call to require_login() will force the user to hit a login screen if they aren’t logged in and will then create a session and will return their uid (you’ll get a uid either way).
NOTE
Users have the option of turning that session into an infinite session by checking the “Keep me logged into [Your App Name]” checkbox on the login screen. See Creating an Infinite Session Key for more information.
$fields = array('last_name', 'first_name');
$myUser = $facebook->api_client->users_getInfo($user, $fields);
Since the Users.getInfo() method requires an array of fields that we want returned, we start off by defining an array called fields and populating it. The call to Users.getInfo() returns a multidimensional array with each item representing one user’s data, but since we passed in only one uid, we can assume that we’re only interested in the data at position 0 when we echo it:
echo $myUser[0]['first_name'] . ' ' . $myUser[0]['last_name'];?>
Discussion
APIs are truly wonderful things. It’s fairly safe to say that they are one of the underlying technologies that makes this entire Web 2.0 revolution possible, and are therefore the golden goose that lays the golden eggs into our fat dotcom v2 pockets. Or, if you want to be a little less cynical, they’re the glue that makes it easy to bind a whole bunch of independent services together into a new mashup app that is far greater than the sum of its parts.
There are a whole bunch of them, too. At the time this was written, the fantastic ProgrammableWeb site (http://www.programmableweb.com/apis), a repository of information about APIs, listed 760 different publicly available APIs in 55 categories. Google Maps is the most popular API for mashups, by quite a long way (47% of all mashups listed on ProgrammableWeb), with Flickr (11%), YouTube (8%), and Amazon (8%) following behind. You can find a whole lot of information about each of those APIs in the directory section of that site (e.g., http://www.programmableweb.com/api/google-maps).
Getting Started with the Client Library
Problem
OK! I’m down with the API scene. How do I get started?
Solution
You need to download a Facebook Client Library in the language of your choice, get it installed on your server, and then get cracking! You’ll find links to various Client Libraries at http://developers.facebook.com/get_started.php, but keep in mind that the only officially supported version is the PHP 4/5 library, and so using another language means relying on third parties to keep the library up-to-date.
In addition to the Wiki (http://wiki.developers.facebook.com/index.php/API), there’s some documentation for the Client Library in the actual code files in the form of comments, and it’s often easier to figure out how to use a function by just looking it up in the facebookapi_php5_restlib.php file than trying to find it on the Web.
Discussion
If you’re running PHP, this should be as simple as downloading the library from Facebook, uploading to your server, and creating a config.php file with your app’s API and secret keys. The PHP Client Library comes with the Footprints sample application, which will give you a very simple overview of an app that uses the API to set some Profile Actions and Boxes. There are also two more demo apps, Restaurants and Who’s Showing Up, which show off Mock Ajax, FBJS, and the Data Store API, and the ability to build apps for users and Pages, respectively. You can find all three apps at http://wiki.developers.facebook.com/index.php/Demos.