How to create facebook application using PHP and graph api

This tutorial will explain how to create a facebook application using Graph api and PHP.For those who are new to Facebook platform you need to learn the basics of Graph api.We are about to build a facebook application you can see a demo here



This tutorial will use PHP code-See PHP tutorials on our site

Introduction to Facebook platform

Facebook allows developers to build applications using facebook platform which  has

FBML – Facebook Markup language

FQL – Facebook Query Language

FQJS – Facebook JavaScript

These are basically derived from standard HTML,SQL and javascript to suit the facebookenvironment.Other than this you need to knoe how the facebook environment is built.Each and every element in facebook is easily accessible from “Graph – API ” which was improved version of REST api which was used previously.

Suppose you want to access the profile picture of a person you can simply call this API

This url simply returns the profile picture of the my facebook account.All you need is the name or UID of the person, in this case you can access my profile picture using my UIDalso.The power of Graph Api is self explained,you dont need to do complex program to do this easy task.

You can easily access information like name,age,gender,location,friends etc I would recommend you to read Graph api documentation.Keep in mind that not all information is accessible you need to get a permission or access token from the user to access it.

Step 1: Create a new application

Our first step to create a facebook application is go to and click “set up new application “.Give your application a name and agree the terms click submit.

facebook applicationfacebook application

Step 2: Fill in the details of the application

facebook application

You will be directed to this page to fill up the essential details of the application.

Step 3: Fill in the facebook integration

Every application has application ID,application secret key and api key unique to every application.You need to fill in the details like canvas page ,canvas url etc.

facebook application

Note: Kindly use “https” in the secure canvas url.
The canvas page is your facebook application url,you can see your application in that URL.

Canvas URL- Each application is just a simple PHP page which will be hosted on your server and the canvas page simply renders the application inside facebook.In this case I have hosted my files at “”
See about canvas

Application architecture

Step 4 : Complete registration

For now click save changes [you can edit details later ]

Step 5 : Download client library

To get started in programming you need the client library.In this case we are developing in PHP hence download php client library.There are other client libraries for Javascript,iOS,android sdk’s [ see here] .


Step 6 :Upload the library

Create a folder in your server and extract the library files there,The “src” folder has facebook.php which will serve as the client library.

Step 7 : Set defaults

  1. < ?php        include_once “src/facebook.php”;   $app_id = ‘YOUR APP ID’;   $application_secret = ‘YOUR APP SECRET’;   $facebook = new Facebook(array(   ‘appId’  => $app_id,
  2.   ‘secret’ => $application_secret,
  3.   ‘cookie’ => true, // enable optional cookie support
  4. ));

Step 8: code

Check whether the user is logged into facebook before loading the application.If the user is not logged in show the $loginurl

  1. if ($facebook->getSession())
  2.  {
  3.   $user = $facebook->getUser();
  4.  }
  5. else
  6.   {
  7.   $loginUrl = “
  8.   &redirect_uri= URL/
  9.   &scope=user_photos”;
  10.   echo ”;
  11.   }

Step 9 : Fetch details

The concept of this application is to display all the profile pictures of your friends in single page

First get the UID of the current user loggin by calling the function getuser()

  1. $uid = $facebook->getUser();  //get the UID of the current user
  2. $me = $facebook->api(‘/me/friends’);   // access all the information of friends
  3. // $me has the JSON detail of all the facebook friends of the current user
  4. echo ” Friends collage”;
  5. foreach($me[‘data’] as $frns)
  6. {
  7. // Display the picture of friends one by one
  8. echo “<img title=”\””.$frns[‘name’].”\”/” src=”\” alt=””>”;
  9. }

Step 10 : Complete coding

  1. < ?php
  2.    include_once “src/facebook.php”;
  3.    $app_id = ‘APPID’;
  4.    $application_secret = ‘APP SECRET’;
  5.    $facebook = new Facebook(array(
  6.   ‘appId’  => $app_id,
  7.   ‘secret’ => $application_secret,
  8.   ‘cookie’ => true, // enable optional cookie support
  9. ));
  10.     if ($facebook->getSession()) {
  11.     $user = $facebook->getUser();
  12.     $uid = $facebook->getUser();
  13.     $me = $facebook->api(‘/me/friends’);
  14.     echo “Total friends”.sizeof($me[‘data’]).””;
  15.     echo ” Friends collage
  16. “;
  17.     foreach($me[‘data’] as $frns)
  18.     {
  19.     echo “<img src=”\””.$frns[‘id’].”/picture\”” title=”\””.$frns[‘name’].”\”/”>”;
  20. }
  21.     echo “
  22.     By <a href=”\”\””><img src=”\”\”” title=”\”Jeyaganesh\”/”></a>”;
  23.     }
  24.     else {
  25.     $loginUrl = “
  26.     &redirect_uri= URL/
  27.     &scope=user_photos”;
  28.     echo ‘<fb:redirect url=”‘ . $loginUrl . ‘”></fb:redirect>’;
  29. }
  30. ?>

After saving the file.Proceed to your canvas URl to see the demo of your application[] you can submit to facebook app directory after 10 people start using your application.

Step:11 Outcome


Access permission for first time user

Download source code





Leave a comment