Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Building a Facebook Application: Part 1

Save for later
  • 4 min read
  • 14 Oct 2009

article-image

A simple Facebook application

Well, with the Facebook side of things set up, it's now over to your server to build the application itself. The server could of course be:

  • Your web host's server – This must support PHP, and it may be worthwhile for you to check if you have any bandwidth limits.
  • Your own server – Obviously, you'll need Apache and PHP (and you'll also need a fixed IP address).

Getting the server ready for action

Once you have selected your server, you'll need to create a directory in which you'll build your application, for example:

mkdir -p /www/htdocs/f8/penguin_pi
cd /www/htdocs/f8/penguin_pi

If you haven't already done so, then this is the time to download and uncompress the Facebook Platform libraries:

wget http://developers.facebook.com/clientlibs
/facebook-platform.tar.gz
tar -xvzf facebook-platform.tar.gz

We're not actually going to use all of the files in the library, so you can copy the ones that you're going to use into your application directory:

cp facebook-platform/client/facebook.php
cp facebook-platform/client/facebookapi_
php5_restlib.php

And once that's done, you can delete the unwanted files:

rm -rf facebook-platform.tar.gz facebook-platform

Now, you're ready to start building your application.

Creating your first Facebook application

Well, you're nearly ready to start building your application. First, you'll need to create a file (let's call it appinclude.php) that initiates the application.

The application initiation code

We've got some code that needs to be executed every time our application is accessed, and that code is:

<?php
require_once 'facebook.php'; #Load the Facebook API
$appapikey = '322d68147c78d2621079317b778cfe10';
#Your API Key
$appsecret = '0a53919566eeb272d7b96a76369ed90c';
#Your Secret
$facebook = new Facebook($appapikey, $appsecret);
#A Facebook object
$user = $facebook->require_login(); #get the current user
$appcallbackurl = 'http://213.123.183.16/f8/penguin_pi/';
#callback Url
#Catch an invalid session_key
try {
if (!$facebook->api_client->users_isAppAdded()) {
$facebook->redirect($facebook->get_add_url());
}
} catch (Exception $ex) {
#If invalid then redirect to a login prompt
$facebook->set_user(null, null);
$facebook->redirect($appcallbackurl);
}
?>

You'll notice that the code must include your API Key and your secret (they were created when you set up your application in Facebook). The PHP file also handles any invalid sessions.

Now, you're ready to start building your application, and your code must be written into a file named index.php.

The application code

Our application code needs to call the initiation file, and then we can do whatever we want:

<?php
require_once 'appinclude.php'; #Your application
initiation file
echo "<p>Hi $user, ";
echo "welcome to Pygoscelis P. Ellsworthy's
Suspect Tracker</p>";
?>

Of course, now that you've written the code for the application, you'll want to see what it looks like.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at $15.99/month. Cancel anytime

Viewing the new application

Start by typing your Canvas Page URL into a browser (you'll need to type in your own, but in the case of Pygoscelis P. Ellesworthy's Suspect Tracker, this would be http://apps.facebook.com/penguin_pi/):

building-facebook-application-part-1-img-0

You can then add the application just as you would add any other application:

building-facebook-application-part-1-img-1

And at last, you can view your new application:

building-facebook-application-part-1-img-2

It is worth noting, however, that you won't yet be able to view your application on your profile. For the time being, you can only access the application by typing in your Canvas Page URL. That being said, your profile will register the fact that you've added your application:

building-facebook-application-part-1-img-3

That's the obligatory "Hello World" done. Let's look at how to further develop the application.