Add This Social Bookmark Button for www.errumm.co.uk errumm monitior image

 << Home

My first experience in building a FaceBook Application in PHP

Initial Links:
http://www.facebook.com/
http://en.wikipedia.org/wiki/Facebook
http://wiki.developers.facebook.com/index.php/Main_Page
http://developers.facebook.com/

Taking a day off from my bustling schedule last week, I set about building a facebook application just to see how difficult it would be. The results to the credit of the FaceBook team were that it wasn't that challenging at all.

I thought I would share some struggles, helpful hints and snippets of code that I came across during construction.

After setting up my application settings, which were all straight forward, I set about creating a simple 'Hello World' page. Following the step by step guide to creating an application provided by FaceBook I wrote a simple index.php page:

<?php
ini_set("display_errors", 1);
require_once("appinclude.php");

echo "<b>Hello World!</b>";
?>

This is where I encountered probably the only major issue of the day which was the following error:

Warning: main(simplexml44-0_4_4/class/IsterXmlSimpleXMLImpl.php): failed to open stream: No such file or directory in /var/www/vhosts/matesbet.co.uk/subdomains/
facebook/httpdocs/facebookapi_php4_restlib.php on line 34

Fatal error: main(): Failed opening required 'simplexml44-0_4_4/class/IsterXmlSimpleXMLImpl.php' (include_path='.:/usr/share/pear') in /var/www/vhosts/matesbet.co.uk/subdomains/
facebook/httpdocs/facebookapi_php4_restlib.php on line 34

The server that I am using runs PHP Version 4.3.9 so of course I had uploaded the PHP4 client library to use with the app. SimpleXML by my understanding is just a simple way of converting XML using different variables and arrays but this particular library was missing on my server so I needed to obtain it. Eventually I found it by simply typing in simplexml44-0_4_4 into google and if you follow the link you will see many people have had the same problem. Instead of installing it on to the server for now I just uploaded it into the directory and changed a few paths so that it could be found.

After that, 'Hello World' popped up.

I found out that if I wanted I could literally use my own PHP code, database, css etc. to build my application without any problems but it made sense to use some of the bits and bobs that were on offer.

Next I set about trying out FBML (FaceBook Markup Language). This proved to be very similar to HTML and contained alot of the usual tags. An example of a fun FBML tag is as follows:

<fb:friend-selector uid="[User ID which is $user]" name="uid" idname="friend_sel" /> Acts like a select but populates it with your users exisiting friends.

A list of FBML tags can be found here and a useful tool for testing your FBML can be found here.

During some database work I realised I needed to obtain some details regarding a particular FaceBook user. This is where FQL (FaceBook Query Language) came into play. Almost exactly the same as SQL, an example of me retrieving a name of a user and putting it into an array using FQL is as follows:

<?php
$q = "SELECT name FROM user WHERE uid='$id'";
$query = $facebook->api_client->fql_query($q);
?>

Check out the FaceBook FQL documentation for more information.

There are a number of requests that can be run in your sripts within the API, that are all found in the included facebook files that you download. Things like sending emails or adding information onto users mini-feeds are amongst them. An example of giving a user a notification is as follows:

<?php
$send_not = $facebook->api_client->notifications_send($id, '....says HELLO....!', false);
?>

A full list of FaceBook API methods can be found here.

As you can see building an application proved to be very straight forward and with a decent array of error messages from facebook when you get tags wrong etc. you can't really go wrong.

Dave Courtney - errumm ltd - 2007

To follow: Developing a facebook application in ASP.Net

© Copyright errumm ltd 2007. Company registration number: 6223097