By continuing to use the site, you agree to the use of cookies.

Continue More information

First Look Tutorial: Facebook iFrame Tab Applications

03 March 2011COMMENTS

Facebook recently announced that, as of March 11th 2011, all Facebook Fan Pages will be moving to a new design layout, similar to that of the new profile pages. Aside from fan pages receiving the new, modern look, finally we get a chance to use iFrames for our applications within fan pages, which is fantastic news for developers as we can finally say goodbye to FBML and FBJS!

So, what's new?

Apart from the freedom of using any web code you like to build your pages, there are some other neat under-the-hood features that are being introduced.

The "signed_request" parameter

As building applications to display in fan pages is almost identical to building canvas applications I thought I'd start with the "signed_request" parameter, as this is a fundamental tool in canvas applications that is now available to us in fan page applications. For those who aren't familiar with this parameter, it is an encoded string passed into your application from Facebook, which contains all kinds of useful information about the user viewing your application. In PHP you would look at the parameter like this:

// Check for the signed_request parameter
if (array_key_exists("signed_request", $_REQUEST))
{
// Grab the signed_request
$signed_request = $_REQUEST["signed_request"]

// Unpack the signed request parameter

list($encoded_sig, $payload) = explode(".", $signed_request, 2);

// Decode the data

$data = json_decode(base64_decode(strtr($payload, "-_", "+/")), true)

// Print the data to screen

print_r($data);
}

This should give you something like this:

Array (
[algorithm] => HMAC-SHA256
[issued_at] => 1299149602
[page] => Array(
[id] => ...
[liked] => false
[admin] => true
)
[user] => Array(
[country] => gb
[locale] => en_GB
[age] => Array(
[min] => 21
)
)
)

The elements to really note are [page] and [user], in particular the [page][liked], [user][locale] and [user][age] - all self explanatory as to what they mean and each one is extremely useful for tailoring what content is shown. In the old style FBML applications the signed_request parameter didn't give you anywhere near as much useful information so this is a major improvement!

As an aside note, if the user authenticates your application you get a couple of extra pieces of useful information - [user_id] and [oauth_token]. If you're using the Javascript SDK then these are usually taken care for you in the background when you make API calls, but in case you need to make some additional calls to the Graph on the server then these will need to be passed in.

Array (
[algorithm] => HMAC-SHA256
[expires] => 1299153600
[issued_at] => 1299149602
[oauth_token] => ...
[page] => Array(
[id] => ...
[liked] => false
[admin] => true
)
[user] => Array(
[country] => gb
[locale] => en_GB
[age] => Array(
[min] => 21
)
)
[user_id] => ...
)

The "app_data" parameter

You can now pass in additional data from the URL into the iFrame tabbed application by appending the URL with an app_data parameter. The parameter can take any string value and will appear in your signed_request data array.

Example: http://www.facebook.com/mypage?sk=app_[app_id]&app_data=hello will give you the following signed_request data:

Array (
[algorithm] => HMAC-SHA256
[issued_at] => 1299149602
[app_data] => hello
[page] => Array(
[id] => ...
[liked] => false
[admin] => true
)
[user] => Array(
[country] => gb
[locale] => en_GB
[age] => Array(
[min] => 21
)
)
)

New iFrame size

The maximum size of contents of your iFrame (if you wish to have no scrollbars) is now 520x800 pixels - the same width as the old FBML applications, but now with a fixed height.

If you wish to not include scrollbars then it's best to use a setup like the following:

<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<meta charset="utf-8">
<style>
body {
margin: 0;
padding: 0;
}
.container {
height: 800px;
margin: 0 auto;
width: 520px;
}
</style>
</head>
<body>
<div class="container">
<!-- Your code goes here -->
</div>
</body>
</html>

All sounds good, so how do I get started?

First things first, you need to setup a new Facebook application, so head over to http://www.facebook.com/developers/createapp.php to get started - you should see a screen like this:

Facebook application registration dialog

Facebook application registration dialog

Once you have entered a name, agreed to the T&Cs and filled in the reCaptcha you should be taken to the application settings page. The two more important tabs that you need to fill in are the "Web Site" and "Facebook Integration" tabs:

Facebook web site settings

Facebook web site settings

In this tab fill in "Site URL" field with the URL which is hosting your application, followed by a backslash.

Next, the "Facebook Integration" tab:

Facebook integration settings

Facebook integration settings

In this tab fill in the following fields:

  • Canvas Page: this unique name of your application canvas on Facebook. The name can be a maximum of 20 characters with only letters, numbers, hypens and underscores allowed.
  • Canvas URL: the URL which is hosting your application. This is probably the same as you entered in the "Web Site" tab, but you can also add any sub-directory structure at the end.
  • Canvas Type: if you are serving your application into a canvas page as well as a tab then keep this on iFrame.
  • Tab Name: this is the name that appears in the left-hand menu on your fan page. The name has a limit of 16 characters.
  • Tab URL: this is the remaining part of the web page address for your application. This could be simply by the filename of your application in your web root, e.g. “myapp.php”, or if your using a framework this should be a route.
Facebook application settings overview

Facebook application settings overview

Once you save your changes you should see something like the above. You’re now ready to start building your first application!

Add your application to a page

If you haven't set up a page for your application you can do so by first going to http://www.facebook.com/pages/create.php.

To add your application to a page click the "Application Profile Page" button on the application settings overview (the button highlighted in the above screenshot). You should be taken to the application's page, which looks very similar to a profile page or regular Facebook page:

Facebook application profile page

Facebook application profile page

On this page find the "Add to my Page" button in the left hand menu. This brings up a Facebook dialog listing all the pages you're listed as an administrator for. Find the page you'd like to add this application to click the button.

That's it! Try going to your page and see you application listed in the left hand menu.

I set up a quick "Hello world" app, which uses the following code as myapp.php:

<!DOCTYPE html>
<html>
<head>
<title>My App</title>
<meta charset="utf-8">
<style>
body, h1 {
color: white;
font-family: Helvetica;
margin: 0;
padding: 0;
}
.container {
background: red;
height: 760px;
margin: 0 auto;
padding: 20px;
width: 480px;
}
</style>
</head>
<body>
<div class="container">
<h1>Hello world!</h1>
</div>
</body>
</html>

It should look something like this:

“My App” in the Facebook “My Page”

“My App” in the Facebook “My Page”

Share.

  • Twitter
  • Facebook
  • Google+
  • LinkedIn
  • Delicious
  • StumbleUpon
  • MySpace
  • Digg
  • Netvibes
  • Tumblr
  • Bebo

Author.

Tom Blakemore
Tom Blakemore (Tech Team Manager)

Comments.

Humaira posted 9th January 2012
At last got the right code. Thanks for sharing.

Add new comment.

You are commenting as a guest (guest comments will be moderated before being published).

Optional: Login below:
*Required fields

Labs are the views and thoughts of staff at Acknowledgement focused around making and doing digital things!

VISIT OUR MAIN SITE
  1. Privacy Policy
  2. T&Cs of business
  3. Website T&Cs
© Acknowledgement 2013. All rights reserved.