Build a Facebook application with Zend Framework

Build a Facebook application with Zend Framework

I’ve just finished a Facebook game, so here a quick tutorial on how to use Zend Framework to build a Facebook Application.

Facebook basics

You can integrate your application with facebook in two way: with an IFRAME (using XFBML) or with FBML.
(If you are already familiar with how a Facebook application work you can skip this step)

You can embed your content in a Facebook page through FBML or use an IFRAME

You can embed your content in a Facebook page through FBML or use an IFRAME

Build your website using FBML tags

Facebook will be a proxy between your server and the user’s browser: it will load a page form your own server like a normal browser, parse it, “enhance” it with his components and then embed it in a Facebook page. You will use standard HTML tags plus FBML tags.


  • You can use almost standard HTML, but with some other tags like: <fb:comments ... />, <fb:wall ... />
  • Feature rich: with a simple tag integrate wall, comments, tabs, dashboard and many others components (Complete list of facebook tags)
  • The url in the browser follow the navigation
  • Faster when you need data from facebook database (groups, friends, etc.)


  • Slower page load times: restart the facebook chat at every request
  • Can’t use standard JavaScript, only FBJS that’s almost equals but your existing scripts and libraries won’t work
  • Its losing ground: Facebook is trying to take IFRAME/XFBML on par

Embed using an IFRAME

Use an iframe: just build a standard web site and let FB embeds it in an iframe


  • Better ajax support: you can use standard javascript, html and css
  • Easy to debug, its just a standard web site
  • Faster page load times


  • Less features than with FBML (but XFBML is rapidly improving)
  • Slower when you need data from facebook database (eg: user’s friends, groups, etc.)
  • The url in the browser doesn’t follow the navigation
  • A bit more complex

We’ll start with FBML since its easier to use and to find help about on the forums.
However take a look at XFBML in the future, since the Facebook team has really improved it recently and they are trying to push them on par.

See facebook wiki for further details on the differences between FMBL and IFRAME.


First of all, setup your FBML app and write down your api key and secret key.

Just follow the onscreen instructions or the wiki, but care attention to the Canvas Page URL:
it is something like:*APPLICATION_NAME* and it is the url through the user can reach your app.

While the Canvas Callback URL (for example: its the address of your own web server, where you’ll deploy your application.
With a dynamic dns service like dyndns you can create a domain name that map to the public address of your development machine, so that you can host your app on your dev machine while allowing Facebook to reach it.

Facebook will map every canvas url:*APPLICATION_NAME*/foo to your callback url:

After you have done with the setup, you can use the official PHP client to interact with Facebook.

First, set up an empty Zend Framework project: you can use the Zend Studio for Eclipse wizard or the new Zend_Tool command line.

Use Zend Studio wizard to create an empty Zend Framewrok project

Use Zend Studio wizard to create an empty Zend Framewrok project

Then, download the client and copy the files in the php folder of the archive to the library/facebook folder of you Zend Framework project.

Now you can start using the client.

The Controller

To start, write in your controllers/IndexController.php:
require_once ‘Zend/Controller/Action.php’;

$err = error_reporting(E_ERROR);
require_once ‘facebook/facebook.php’;

class IndexController extends Zend_Controller_Action {
public $apiKey = “22c…s05”;
public $apiSecret = “df6…fb5”;

function indexAction() {
$facebook = new Facebook($this->apiKey, $this->apiSecret);
The line: $err = error_reporting(E_ERROR); is needed to hide some strange php notices from the facebook.php file.
In the line 12 we instantiate our client with the api key and secret taken during the application set up.
With the require_login method you can require the user to “add” your application.
Notice that many api features are available only if the user has added the application.

Then in your Zend actions you can call every standard method of the api with the
$facebook->api_client variable, for example:
$facebook->api_client->notifications_send(…); //Send a notify to a user
$facebook->api_client->friends_getAppUsers(); //Get the friends of the current user that use your app
See the complete list of methods on the wiki and try them with the API console tool.

The View

Now that you know how to interact with Facebook, you can show a simple page containing the “Comments box” component.

In your layouts/main.phtml file write:
headScript() ?>
headStyle() ?>
layout()->nav ?>

placeholder(‘title’) ?>

layout()->content ?>
As you can see we don’t have to put the standard html opening tags in your layout (<html><head>...) since Facebook will load the page from your server and embed it in his own HTML canvas page.
We had also used our first FBML tag: <fb:title> to set the page title in the browser titlebar (we can’t use the ZF headTitle() method here).

Now create a view script for your index action of the index controller views/scripts/index/index.phtml:
placeholder(‘title’)->set(‘My First Facebook App!’); ?>


Marco D'Alia

Comments (5)

  • Pingback: // popular today

  • Pingback:

  • Brian Reich

    Great tutorial!!! I’ve been waiting for something like this for a long time.

    May 11, 2009 at 2:20 pm
  • Chris

    What about using Zend_Rest_Client?

    June 3, 2009 at 11:37 pm
  • Madarco

    @Chris: I haven’t tried to use Zend_Rest_Client with the Facebook api, however I think the official fb php client has some features that the Zend_Rest_Client doesn’t have, like the session_key and facebook authentication management or the requireLogin method.

    June 4, 2009 at 8:54 am

Comments are closed.