In this tutorial I am going to show you how to create a simple Facebook application.

Getting the basics

First of all you'll need to install the developer application on Facebook. To do this log in to your Facebook account and visit this link here to install the Developer application. You should be redirected to this screen:

Now you've installed the developer application you will need to create your first application. Click on the 'Apply for a key' and fill in the necessary details.

Give you application a name. In this example I have called mine 'My Test Application'. For the two email fields you should put in your email. Now for the callback URL. This is the directory on your host containing the files. Your application should definitely have it's own folder on your host (if not Facebook users will probably be able to access files you don't want them too). I've entered mine as http://www.urbanoalvarez.es/facebookApp/. All that's really left to do is your Facebook canvas page URL. I'm going to use aSimpleTestApp for mine. You can use anything you want as long as it's available. You shouldn't need to edit anything else. Just click 'Submit' to create the application.

You should have been redirected to the previous page only now you will see you have created an application. Note down the secret question, api key and your callback URL because you will need them in the next stage.

Now you've created your application you will need to install the Facebook Platform libraries on your host. You can download them here:
http://developers.facebook.com/clientlibs/facebook-platform.tar.gz

Once you've installed the libraries (read the README if you need any assistance) then you can start coding. First of all we need to create a config file which initiates the API and allows us to make a connection with Facebook.

The actual code

< ?php
   require_once 'client/facebook.php'; // Path to Facebook.php (either inside 'client' or 'php4client')
   $appapikey = 'apikey'; // Enter your API key you noted down earlier
   $appsecret = 'yoursecret'; // Enter your secret you noted down earlier
   $facebook = new Facebook($appapikey, $appsecret); // Initiate client
   $user = $facebook->require_login(); // Authorize current user
 
   $appcallbackurl = 'http://www.drcrazy4.info/fbapp/'; // Your callback URL
   // Has the logged in user installed the application
  try{
     if ($facebook->api_client->users_isAppAdded() == ""){ // If the application hasn't been added
          $facebook->redirect($facebook->get_add_url()); // Redirect the user to the add applciation link
     }
  }catch (Exception $ex){
         $facebook->set_user(null, null); // Clear current application cookies
         $facebook->redirect($appcallbackurl); // Redirect user to application
  }
  ?>

Save that file as config.php.

Now we have the authorization done we can get productive. For this application we're going to create a simple hello world application (I know how boring. In later tutorials I will discuss things like friends, photos etc.). Here is what's left:

< ?php
   include 'config.php'; // The file we made just now
 
   $echo = 'Hello there ' . $user; // $user is the logged in user. This will be displayed on the main application page
   $fbml = <<<EndHereDoc
   Hello world!
   EndHereDoc; // This is what will be displayed on users profile pages.
 
   $facebook->api_client->profile_setFBML($fbml, $user);
   // Set the profile box content for the logged in user
   ?>

Save that file as index.php.

Now test your application. You should see 'Hello world!' in a box on your profile and on the main application page you should see 'Hello [USERNAME]' ([USERNAME] being your current username). In later tutorials I will discuss a little more about FBML and how you can use the client libraries to get users friends etc. Tutorial source iscripting.com

If you have a question please feel free to ask.