Facebook Apps – Hello World

Today, for one reason or another, I’ve spent sometime playing with Facebook Apps. The last time I played with Facebook was a good couple of years ago, and could remember it being a pain, not difficult, just a pain.

To start from common ground, there are likely to be some steps you’ll be able to skip, but for completeness:

1. Create a Facebook account. (Yep, I don’t (or didn’t) have one- FWIW I don’t intend to keep it much longer than finishing this first app).

2. You need some kind of PHP hosting environment. I signed up for a free account at http://www.zymic.com. There are probably others, maybe better ones, but I found it ok, and ultimately it was free!

3. Download the Facebook SDK, which is essentially a zip file containing the require php files. I uploaded the 3 .php files (facebook.php, facebook-mobile.php and facebookapi_php5_restlib.php to the root folder of my site) and also created the directory structure for the ‘jsonwrapper’ and sub-files and sub-dirs.

4. Go back to Facebook and add yourself as a developer. Then create an app, playing special attention to the API Key and the APP Secret which are generated at this stage. You also need to specify the URL of the site that is hosting your php file, as well as the Canvas URL address (which I set as gbtesterapp).

A summary of these settings is in the image below:

5. Now we need to create the index.php at the location specified in the application settings. (Its possibly worth noting it may be worth creating a simple ‘HelloWorld’ php script to prove the php server is behaving correctly).

The following is just some code to output the currently authenticated users Unique ID and their name.

require_once 'facebook.php';

// API Key
$appapikey = 'APIKEY';

// app secret
$appsecret = 'APPSECRET';

$facebook = new Facebook($appapikey, $appsecret);
$user = $facebook->require_login();

// callback URL
$appcallbackurl = 'http://gregbowie.zxq.net/';

try {
if (!$facebook->get_loggedin_user()) {
} catch (Exception $e) {
echo "in exception";
$facebook->set_user(null, null);

echo "<p>hello $user</p>";
echo "<p>hello <fb:name uid=\"$user\" useyou=\"false\"/></p>";

Whilst not difficult, for some reason I went round the houses with this, and spent quite sometime on it. I’d like to point of the following references which helped me get to where I am now:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s