Zend Framework and Facebook: writing a simple facebook application in Zend Framework

31 May

In the social networking era, who don’t know about facebook. One of the famous and widely adopted social networking applications, where you can connect to your friends and family.

One the other hand Zend Framework is widely adopted open source MVC framework build in PHP.

Facebook has its own API classes for interacting with application. It also has language called FBML facebook markup language, FBJS facebook javascript for using javascript in facebook application, FQL, facebook query language for fetching data from database. Keep in mind that FBML is subset of HTML with some elements removed, and some added that are specific to facebook application.

Zend Framework on the other hand shipped with its own component like Zend_Auth for authentication, Zend_Acl for Access Control List, Zend_DB for connecting and working with database, Zend_Form for creating html forms and so on.

One of the nice things about Zend Framework is the configuration overhead and the use of component that are not tightly coupled.

To have a good understanding of both, I’d suggest you to read the documentation of both.

In this article I’m going to discuss how you can create a simple facebook application in Zend Framework.

Before Starting, download Zend Framework, create facebook application using faceboo.com/developers and download client library.

Once you successfully downloaded the facebook library. Next step is to create an appropriate directory structure.

I have the following directory structure.

Directory structure for creating facebook application in ZF

Directory structure for creating facebook application in ZF

well, don’t over think. The most important are application directory containing all our controllers, models and views.

The library directory contain two folders, facebook-platform that contain the facebook client library and Zend directory contain all the components shipped with Zend Framework.

The most important file here is index.php which serves as bootstrap file, getting all request from users and route them to specific controller/action.


The code you will need to include in this bootrap file(index.php), look like this.


require_once ‘facebook-platform/php/facebook.php’;

$appapikey = ‘dc42122221bc005c9d1153404a39a32667’;

$appsecret = ‘3e5c872ba5c7wew33204f2d153503f37’;

$appcallbackurl = ‘exmaple.com’;

$facebook = array(

                                    ‘appapikey’ => $appapikey,

                                    ‘appsecret’ => $appsecret,

                                    ‘appcallbackurl’ => $appcallbackurl




require_once “Zend/Loader.php”;



$registry = Zend_Registry::getInstance();



$frontController = Zend_Controller_Front::getInstance();





This is the minimum code required.

The most important lines for facebook application are first few lines, where we are using require statement to include facebook.php file. Define a variable having own facebook application key, secret key and back url. We define an array and store all these information.

Once array of these facebook variable has been defined we get instance of Zend_Registry and set “facebook” to facebook array defined earlier for future use.

Next we get instance of front controller, set controller directory and call method dispatch.

That’s it. We have now properly defined our bootstrap file.


Next step is to create our first controller and define template files.

Go to application/controllers/ and create a file called IndexController.php and write the following code in it.



class IndexController extends Zend_Controller_Action



            public function indexAction()


                        $face = Zend_Registry::get(‘facebook’);

                        $facebook = new Facebook($face[‘appapikey’], $face[‘appsecret’]);

                        $user = $facebook->require_login();

                        try {

                        // If app is not added, then attempt to add

                        if (!$userId = $facebook->api_client->users_getLoggedInUser()) {



                        } catch (Exception $ex) {

                                    // code for handling exception


                        $friends = $facebook->api_client->friends_get();

                        $this->view->friends = $friends;

                        $this->view->user = $userId;





Here we have inherited our class from Zend_Controller_Action. And defined a single action named indexAction.


The first line

$face = Zend_Registry::get(‘facebook’);

return an array we earlier set in our bootstrap file. This array contain application key, secret key and call back url.

Next we create facebook application object, giving it application key and secret key.

Once we created the object, we call require_login() method. This will force user to logged in to facebook before moving forward.

Logging in is compulsory for fetching information from the facebook application.

Next we check if user has logged in by calling users_getLoggedInUser(). If not logged in we redirect user to the facebook log in page.

This user_getLoggedInUser() return ID of the current logged in user.

Next we get the friends of the user by using another useful method friends_get().

After getting this information, we assign it to our view as.

$this->view->friends = $friends;

$this->view->user = $userId;

That’s it. This is our simple controller/action.

Next step is to create our view and put the code we want.

For this, go to application/views/scripts/index/index.phtml. If this file has not been created, create one and put the following code in it



echo “<p><h4><fb:name uid=\”$this->user\” useyou=\”false\”/></h4></p>”;

echo “<p><h5>You have following friends</h5></p>”;

echo “<ul>”;

foreach ($this->friends as $friend) {

            echo “<li><fb:name uid=\”$friend\” useyou = \” fasle \” />”;


echo “</ul>”;


To have a look what this is the output of this, browse


Any query and suggestion would be welcomed.


6 Responses to “Zend Framework and Facebook: writing a simple facebook application in Zend Framework”

  1. Mahmud Ahsan June 8, 2009 at 8:30 am #

    yeah, nice tutorial, i’m also use zend framework to develop all of my client’s or personal facebook application. 🙂

  2. Ben October 16, 2009 at 3:33 pm #

    Thank you for this tutorial, just made a Facebook Connect with your help! Thanks.

  3. sudeep February 19, 2010 at 5:49 am #

    hi wat abt d file under modules

  4. Qasim Ali Khawaja March 29, 2010 at 10:34 am #

    Your specified link for the app is down 😦

  5. dhanesh mane October 30, 2010 at 4:12 pm #

    hey ben, There is nothing much about how to integrate FB connect with Zf based website, will you please let me know how you did it?

  6. issamux November 10, 2010 at 3:20 pm #

    nice tutorial
    but …. it use an old Facebook api and the link is down also

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: