Mohammad Emran Hasan
Mohammad Emran Hasan

Mohammad Emran Hasan

Quick start on new Facebook PHP SDK (IFrame based)

Quick start on new Facebook PHP SDK (IFrame based)

Mohammad Emran Hasan's photo
Mohammad Emran Hasan
·May 3, 2010·

3 min read

This post is more than couple 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:

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.

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 "top.location.href = '$url';";

} else {

try {

    $uid = $facebook->getUser();
    $me = $facebook->api('/me');

    $updated = date("l, F j, Y", strtotime($me\['updated\_time'\]));

    echo "Hello " . $me\['name'\] . "  

"; echo "You last updated your profile on " . $updated;

} catch (FacebookApiException $e) {

    echo "Error:" . print\_r($e, true);

}

}

You might be wondering – it’s pretty straight-forward, so what’s the catch ? Well, to be honest, the documentation does not have the “canvas” parameter mentioned anywhere which does the primary magic here. Also, if you do not use the javascript trick, then you end up with an authorization dialog with full facebook UI within the iframe itself (see below).

CodeIgniter Version

Here is the CodeIgniter version of the above example. The significance is that CodeIgniter removes the values from the $_GET super global – which is required for the library to work. We thus re-populate it on the constructor ourselves and start a session to share data among subsequent page visits.

facebook = new Facebook(array( 'appId' => $this->config->item('facebook_app_id'), 'secret' => $this->config->item('facebook_secret_key'), 'cookie' => true, 'domain' => 'phpfour.com' ));

    $session = $this->facebook->getSession();

    if (!$session) {

        $url = $this->facebook->getLoginUrl(array('canvas' => 1, 'fbconnect' => 0));
        echo "top.location.href = '$url';";

    } else {

        try {

            $uid = $this->facebook->getUser();
            $me = $this->facebook->api('/me');

            $updated = date("l, F j, Y", strtotime($me\['updated\_time'\]));

            echo "Hello " . $me\['name'\] . "  

"; echo "You last updated your profile on " . $updated;

        } catch (FacebookApiException $e) {

            echo "Error:" . print\_r($e, true);

        }
    }
}

}

Hope I am able to help a few people.

Cheers!

 
Share this