Setting up our application
Before we start you will need to have a facebook account and a testing area on the internet. To set up our app we must first get our facebook API key and the secret code. To do this go to FaceBook Developer Section and in the top right corner of the screen you will see a button "Set Up New Application"
On the create application screen all you need to do here is enter a title for your app and accept the terms and conditions then click "Create Application".
On the next screen you will have all of your application settings as well as your api key and secret.
We need to set the canvas page url and the canvas callback url. The canvas callback url is where facebook will look for out app.
We need to change how facebook renders our application so click on the canvas tab on the left hand side and under canvas settings you will see "render" change this from IFrame to FBML (Facebook Markup Language).
We can now start coding out application.
Coding the application
After saving the changes you make you will be sent back to "My Applications". If you scroll down to the bottom of the page then on the left you will see a link to download the client library. Download the client library and extract the files (You should upload these files to your web server to the directory which you specified when you entered the canvas callback url). If you browse the files you will see the facebook php client library as well as a sample application, We are only going to be paying attention to the php client library.
Create an index.php file and in this we need to include the file php/facebook.php and set out api key and secret in a variable.
require_once( "php/facebook.php" ); $api_key = "ENTER YOUR API KEY HERE"; $secret = "ENTER YOUR SECRET HERE";
Next we initiate the facebook class and we can start using the api.
require_once( "php/facebook.php" ); $api_key = "ENTER YOUR API KEY HERE"; $secret = "ENTER YOUR SECRET HERE"; $facebook = new Facebook( $api_key, $secret ); $user_id = $facebook->require_login();
Now who ever visits your app $user_id will be set to their id. We can use this with FBML to get the username.
require_once( "php/facebook.php" ); $api_key = "ENTER YOUR API KEY HERE"; $secret = "ENTER YOUR SECRET HERE"; $facebook = new Facebook( $api_key, $secret ); $user_id = $facebook->require_login(); echo "Hello World"; echo "Current logged in as <fb:name uid=\"$user_id\" />";
If you now upload your index.php file to the same dir as the client files and visit your app on facebook (You will have to allow access to the app) then you will hopefully see a hello world message.
Check here this simple Facebook application (you need to login to Facebook first).
Facebook resources for developer
Here's a few links to documentation where you can ready more into the api and FBML. Remember to keep and eye on the site as we will be posting another facebook api tutorial as part of the api series which will take a more in depth look at creating facebook apps.