Use facebook API with CakePHP
Facebook API with CakePHP
Making a Facebook application is all the craze nowadays with new ones being posted every hour. Honestly, it seems more like a flood of mediocre applications but the more the merrier I guess. At first, I believed there was great potential for full-blown Facebook applications due to the ability to spread and network. However, it seems the most successful apps (by user count) are small widgets that users find interesting. Still, the platform is young so only time will tell what comes out of Facebook. It’s probable that a Google Docs-like application is made for Facebook which serves as the new word processor or Facebook calendar serves as the new PIM. If web applications are the future, Facebook obviously has huge potential.
I’ve messed around with the API and it really is easy to get started. You can use a plethora of different development languages but I obviously used PHP, more specifically CakePHP. Here’s how you can easily start a Facebook application with CakePHP.
This is more an entry on vendor code snippets, which will greatly add functionality to your CakePHP application. Other really useful code snippets you can plug in include the collection of Zend Framework components, which has support for APIs such as Flickr, Audioscrobbler, and del.icio.us. These are more or less a collection of functions that you can call elsewhere from inside your CakePHP code. In a sense, it is rather procedural (basically an include).
Download the Facebook API and extract it. The only code you will take from here is the /client folder (assuming you are using PHP5) or the /php4client folder (if you are using PHP4). Create a /facebook folder in your /vendors folder in your CakePHP application. Drop the files from the Facebook API into this new facebook folder. So the path to facebook.php is /vendors/facebook/facebook.php
To use a vendor file, use the following code in your controller (don’t include the .php extension):
Therefore, to use the file facebook.php:
Then create a new Facebook object:
$facebook = new Facebook($api_key, $secret);
Obviously, set those two variables before this statement. You can get those variables from your Developer application in Facebook.
Then, you’re off to using the Facebook API! You’ll probably want the user to login to his/her Facebook account.
$facebook_user_id = $facebook->require_login();
This will redirect the user to the Facebook website and show a login if needed. Once they are logged in, they will be redirected to this previous page. However, make sure that your page is under the directory file structure of what you put as the Callback url for your application settings. Otherwise, Facebook will redirect to your callback url/previous file which will be something like http://yourcakephp/http:… This function will return the Facebook id of the user.
If you are building an internal Facebook application (one that runs within Facebook), for your default layout, you will want to remove all the html wrappers. A good way to start is to change your default.thtml to just
<?php echo $content_for_layout; ?>
You will want to take advantage of FBML which you can use in your views or use Facebook iframes to serve your application.
Now you can use the Facebook API as needed.
$facebook_users = $facebook->api_client->fql_query(‘SELECT first_name FROM user WHERE uid=’ . $facebook_user_id . ); echo ‘Hello ‘. $facebook_users[‘name’] . ‘!’;
The easiest way to find what the PHP Facebook functions are is to look at the file facebookapi_php5_restlib.php. Facebook did a good job documenting the functions so it is really easy to use.
You can use other external APIs in CakePHP just like this. Drop the file into the /vendors folder and use the vendor() function to include it. Then you can use it just as if you used an include
Well there you have it folks, let me know what you think in the comments below