Write your first FaceBook App!
I recently tried to get my hands dirty with Facebook application development platform. I know there are a bunch of resources available on the web to help you in the process, but when I was trying get my tiny app to work, I realized there were a few things which aren’t explained too well on http://developer.facebook.com/. I am writing down the step-by-step guide that you can use to create your first Facebook application and join the club!
Before you start creating application, here is in brief, how Facebook communicates with your application:
- User accesses your application using a url like http://apps.facebook.com/yourapp
- This url invokes the callback url that you specify while creating your app (explained below). This url essentially where your app is hosted, e.g. http://yourserver.com/facebookapp.
- Your application can serve either html (iframe app) or FBML. In case you have decided to use FBML, the content is parsed by Facebook and then rendered back to the client after converting it to html.
Following are the steps to create your first Facebook app. For the sake of brevity I am not duplicating information which is already present somewhere else:
Create/Register your app with Facebook
- Add the Facebook Developer Application to get started.
- Read this article and follow the steps to add a new application:
- Once you have created the application, you should have your api key and the secret key
Implementing/Deploying your application
We are going to create a simple app that prints the name of the user and the current time (big deal!). The example below also illustrates how to leverage Ajax support in Facebook apps.
- You can use multiple languages to implement your facebook app. Officially only java and php are supported, but there are implementations available for other languages as well. Checkout the resource page.
- If you already have a webserver than can host your application, you can skip to the next point. For the rest, you’d need to get a hosting service. If you’re just trying to get a feel of Facebook app, you may want to start with php (no I am not a php fan - in fact I had not written a single line of php code till last week) for the simple reason that it’s easy to find free/cheap php hosting sites.
- [Update: If you're writing the app in java, check this out]
- Create the php file corresponding to the call back url you registered while creating the application. The following shows how to get user information, and print current time.
<?php
//replace fb-phplib-root by the directory having
//the facebook client api library
require_once('fb-lib-root/client/facebook.php');
$api_key = "your-api-key";
$secret_code = "your-secret-code";
$facebook = new Facebook($api_key,$secret_code);
//get logged in user id
$fb_user = $facebook->require_login();
//use fb:name to get the user name
echo "Hi <fb:name uid='".$fb_user.
"' useyou='false' possessive='false'/>!";
$facebook->api_client->fbml_refreshRefUrl(
"http://yourserver.com/default_fbml.php");
$facebook->api_client->profile_setFBML(
"<fb:ref url='http://yourserver.com/default_fbml.php' />");
?>
//show the default fbml on canvas as well.
//fb:ref tag gets the file content.
<fb:ref url="http://yourserver.com/default_fbml.php" />
<?php
//uncomment the following to debug the facebook object
//echo "<pre>Debug:" . print_r($facebook,true) . "</pre>";
?>
Here is what default_fbml.php contains. It shows the time and has a Refresh link that gets time from server using AJAX (I know what you’re thinking, but let’s leave the more complex stuff for the second Facebook app :)):
<div style="padding:10px;">
Current time is :
<span id="time">
<?php echo date("d/m/y, H:i:s",time()) ?>
</span>
<a href="#" onclick="fetchTime(); return false;">
<span id="action">[Refresh]</span></a>
<br />
</div>
<script><!--
function fetchTime() {
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.ondone = function(data) {
document.getElementById('time').setInnerFBML(data);
}
ajax.requireLogin = 'true';
ajax.post('http://yourserver.com/timeserver.php');
}
//--></script>
The ajax requests go back to timeserver.php. This file simply echoes back the current time:
<?php
echo date("d/m/y, H:i:s",time());
?>
Caveat with fb:ref
The file specified in <fb:ref>
If you are planning to use a cron job to update, for example, the default profile page, you would invariably run into the issue of invalid session key. In order to fix this, you need a key that does not expire. Look here to get more information.
Hope you found the post useful! Comments/suggestions are welcome! I will soon post another example using Java client libraries.
Useful links and resources:
Facebook developers’ Wiki
Download Client Libraries
Facebook REST interface
FBML
Next Steps »
- Leave your comment (14)
- Trackback from your own site
- Read other posts in similar categories : Facebook, How-To, Technology






Subscribe by Email
Hey!…Man i love reading your blog, interesting posts ! it was a great Tuesday
Hi,
Good, lucid article. Please could you post a similar one for Java as early as possible?
Thanks Andy! Good to know you liked the posts!
Shalini, there is a post on getting the authentication piece to work using Java. Once you have that working, it should be pretty straight forward. Here is the link:
http://theliveweb.net/blog/2007/10/31/facebook-authentication-using-java/
Hello webmaster…Thanks for the nice read, keep up the interesting posts..what a nice Wednesday
Hi…Thanks for the nice read, keep up the interesting posts..what a nice Wednesday
I couldn’t understand some parts of this article …, but I guess I just need to check some more resources regarding this, because it sounds interesting.
Good first approach to Ajax in Facebook!
Hey!
Well i finally decided to go with PHP and have the makings of an app!
I’m facing an error though and if you could point me to a solution, it’ll be very nice.
Call to a member function on a non-object in facebookapi_php4_restlib.php on line 1397 is what i get. And its really upsetting because i got the same error on a call to two things:
1. $facebook->api_client->profile_setFBML
2.facebook->api_client->feed_publishActionOfUser($feed_title,$feed_body);
I have put this up on the facebook developer forum but no real replies there. You seemed to have comfortably used api_client calls well, so please guide me.
Thanks,
Shalini
Eat a third and drink a third and leave the remaining third of your stomach empty. Then, when you get angry, there will be sufficient room for your rage.
Man, you really saved me getting fitted for a straight-jacket tonight. I’ve been poking around the FB developer docs/forums for days and haven’t found anything remotely as useful as this walk-through. Thanks so much for publishing this. I read the docs on fbml_refreshRefUrl at the official site several times and the way they explained it was just confusing as hell. Kudos to you.
i copied the above code and run it but it causes some errors like this
Warning: require_once(fb-lib-root/client/facebook.php) [function.require-once]: failed to open stream: No such file or directory in /home/onixtech/public_html/magicspell/default_fbml.php on line 18
Fatal error: require_once() [function.require]: Failed opening required ‘fb-lib-root/client/facebook.php’ (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/onixtech/public_html/magicspell/default_fbml.php on line 18
Thanks for the great post here
Hi,
we are new developers writing application on facebook. We are having hard time to show up our profile on boxes tab. The profile shows up fine on wall but when we tried to move it to boxes tab we don not see the hrml all it says is
No content to display.
This box will not be visible to people who view your profile until this application adds content to it.
Can anyone pleas elet us know the exact steps to write/create profile on boxes tab with some simple example.
Regards,
VB