Facebook Application Development (friends invite, publish to wall)

  |   By  |  0 Comments

The trouble with tutorials on Facebook Application development is that they go out of date so quickly. This one was written 14th Dec 2010. The Facebook developers wiki is pants too!
So here’s what I learnt the last few days.

I’m assuming you have registered your application to be on http://apps.facebook.com/developers
and have the vital application id, application secret and application key.

The canvas is where your app shows within facebook.com – choose Iframe in the “Facebook Integration” settings.
The application page will look something like this, but have values;-)

The canvas url is the directory on your server where your application is hosted.

So firstly you need your users to add your application. I do that with fbmain.php
Which requires facebook.php downloaded from the sdk area.

        echo '</pre>
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,

//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update'

$fbme = null;

if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';";
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');

} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';";

function d($d){
echo '
        echo '</pre>';


Then in index.php you can start coding…

//Your app code here
print_r($fbme); //gives you the users info in a nice array!

The header.inc.php contains a call to the javascript sdk you may need.

    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js">
     <script type="text/javascript">
         appId  : '',
         status : true, // check login status
         cookie : true, // enable cookies to allow the server to access the session
         xfbml  : true  // parse XFBML
         function increaseIframeSize(w,h){
                var obj =   new Object;

and footer.inc.php just closes the body and html

If you want to add a invite friends dialogue – this was a huge pain to google!!!


//in case you want to call invite to help the viral along using  http:/apps.facebook.com/yourappname/invite.php
{ //invite section
        echo "

Blah Blah Blah

?><script type="text/javascript">increaseIframeSize(700,1700);
Message you want on the top of the friend request
//end invite section }

Just call that as require(“invite.php”) where you want the invite to go – the sizing javascript stops those horrible scrollbars on your iframe.

If you want to publish something on their status updates…

//Publish to wall
$args = array('access_token' => $session['access_token'],
              'message' => 'Main message text',
              'name'  =>'Title of messagee',
              'link'  =>'http://apps.facebook.com/yourapplication',
              'picture'  =>'A nice logo image on your server!'
$ch = curl_init();
$url = 'https://graph.facebook.com/me/feed';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
//End publish to wall

Now you have the basic building blocks for building your facebook app. Start coding! and stick it in index.php after the yoru app code goes here comment!

Have fun and try my app

http://app.facebook.com/yourmotisdue – 28% of us forget to get our MOT done on time, so my app will send you an email reminder a month before to get it booked in!