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:
| <?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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?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); } } |
(more…)
Recent Comments