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.

 
';
        print_r($o);
        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(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update'
)
);

$fbme = null;

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

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

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

}

Then in index.php you can start coding…

 
<php
require("fbmain.php");
if($fbme)
{
require("header.inc.php");
//Your app code here
print_r($fbme); //gives you the users info in a nice array!
require("footer.inc.php");
}
?>

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">
       FB.init({
         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;
                obj.width=w;
                obj.height=h;
                FB.Canvas.setSize(obj);
            }


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!!!

invite.php

 
<?php
if(!$fbme)
{
//in case you want to call invite to help the viral along using  http:/apps.facebook.com/yourappname/invite.php
    require_once("fbmain.php");
    require("header.inc.php");
}
if($fbme)
{ //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…

 
<?php
//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);
curl_close($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!