3/05/2010
This post is more than 1 year old. It solved the issues when the SDK was first released and facebook had lack of documentation. However, now facebook has very rich set of
documnetation and you should follow that instead!
The new Facebook API has already spread over the application developers and if you’re like me, you’ve already got your hands dirty to see how this new thing works. If you have tried to follow the documentation to authorize/get session in your canvas application, it is likely you have already hit roadblocks. Well, I am no savior but I have glued together a few clues and got it working for myself.
I am assuming that you have already created your application by following the Getting Started section from the official documentation. Also, this is for IFrame based applications only.
Enough talking, let’s get some code.
Step 1: Get the new SDK
Download the new SDK from github. We will only need the facebook.php file from the src folder. In our project directory, let’s create a folder called “lib” and put the file there.
Step 2: A configuration file
Let’s now create a configuration file to store our facebook configuration. Let’s name it config.php. Here goes the source:
<?php
define("FACEBOOK_APP_ID", '113795715321151');
define("FACEBOOK_API_KEY", '064baf5fb98de050cd7b9a001ca1988b');
define("FACEBOOK_SECRET_KEY", '430f43c01f6dfe02c284b4545976f9ce');
define("FACEBOOK_CANVAS_URL", 'http://apps.facebook.com/emran-test-app/');
Step 3: Application Main Page
This file will be the main entry point to our facebook application. It just instantiates the facebook object, sets the configuration and checks for a valid session. If it does not find a valid session, it redirects to the login page. For first time visitors, it will be the authorization page. On later requests, the operation will occur in the background – without any user interaction.
<?php
include_once 'lib/facebook.php';
include_once 'config.php';
$facebook = new Facebook
(array(
'appId' => FACEBOOK_APP_ID
,
'secret' => FACEBOOK_SECRET_KEY
,
'cookie' => true,
'domain' => 'phpfour.com'
));
$session = $facebook->getSession();
if (!$session) {
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0
));
echo "<script type='text/javascript'>top.location.href = '$url';</script>";
} else {
try
{
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated;
} catch
(FacebookApiException
$e) {
echo "Error:" . print_r($e, true);
}
}
Read the rest of this article »
24/08/2009
The first ever “Facebook Developer Garage” took place in Dhaka, Bangladesh and I am proud to be a part of it. I was one of the speakers of this spectacular event organized by Facebook and IBT bangladesh. I spoke about the recently released Open Stream API from Facebook. It was more of an overview rather than a detail one. Here goes the slides:
I have created a quick start example which outlines both Facebook Connect and the Open Stream API. You can check that here. A blog post covering the steps is coming soon in 3-4 days.
Stay tuned if you have interest in them.
Cheers
20/01/2008
Although its a bit late to post a "happy new year" entry, I want to thank all the visitors who paid a visit to my blog last year and wish you a Happy New Year ahead. It’s been a great year with many ups & downs in my life, alas my short term memory won’t allow me to write a post like Hasin. Nevertheless, I am finally enjoying a planned life with time for my job, my company, my family and most important – myself !!!
Last year I set a number of goals – some were achieved and some were not. This year, I want to pressurize myself a bit by keeping those goals public in my blog. This will push me to walk in the right path as people will often ask about them and many of them won’t accept excuses. So, here goes my public commitment for 2008:
- I will start my Masters in Business Administration (MBA)
- I will sit (and will hopefully pass!) the Zend Certified Engineer (ZCE) exam for PHP5
- I will write at least 4 posts per month (48 in the year – a lot considering my track record!)
- I will be working with a number of libraries in Orchid framework
- I will release at least one Facebook App (personally, not from my job!)
And yeah, I’ve changed the blog theme, thanks goes to Dezinerfolio – i was looking for a dark theme for a long time. I did some modification to their theme, and added a pagination like them. Hope you guys enjoy it more. Drop me a line if you have any ideas, suggestions, views, complaints – anything!