Create Your First Facebook applications using PHP SDK

Working with Facebook API Part-1:Create Your First Facebook applications using PHP SDK


In this article we will demonstrate how to create a simple application for facebook using the Graph API with php sdk. Let’s create a application for facebook that show your Public Profile. We will assume that the application will be hosted on a particular server itself.

If you do not have your own server, you can configure the application to run locally in your environment during its development (see how to do this later). When you need to publish it to production, it will have its own server.

Register as a developer account

The first step to create a facebook application, is recorded as having a developer account account. If you have not created any application, this will be your first step. Access the link and click the Register Now! . See figure:

PHP FaceBook Application

After clicking the Register as a Developer link, just follow the directions step by step:

  1. Accept the terms of membership
  2. Report details on your user page (you can skip this step if you do not want to enter anything)
  3. Information that your account is now a developer account

See Figures: 

Facebook Developer Account

Facebook Developer Registration Process Step 1

Facebook Developer Registration Process Step Final

If necessary facebook will check your account by sending a token to the number of a cell yours.

From this point we are able to create the first application.

Creating a new application

Access the link and click the + Create New App . See the figure below:

Creating a new application

The next screen tells you the basic details of your application:

Creating a new application details


  • Informed only the fields Name Application (See I informed Initial App ) and App Category (See I informed Apps for Pages ) that are sufficient to test your first app. In the Name field, enter the appropriate name of your application, it will be used within the application environment of facebook.
  • Keep the option Lodging disabled because we are hosting our own application on a local server or even in the event of development.
  • To complete this initial process of creating the application, enter the captcha requested on the next screen.

Configuring the newly created application

At the end of the previous step to create the application, you will be redirected to the editing application settings, as shown below: (Remember that you can return to this screen whenever you need to re-configure any parameter, simply login https :/ / / apps and click the Edit Application button)

Configuring the newly created application


  • Note the appId and secret gifts on the top screen. These are the data that identifies your application and will be used in the same schedule.
  • Click on the tab Site with Facebook Login and enter the domain name where your application will be hosted. No need to give the full path where your final script is located. For example, you have created your handy php script on the link , please just .
  • Another point to pay attention is that if you are using a communications port on your domain other than the default 80, you should also inform the door. For example, if port 8080, please
  • If you want to test your application in your own local development environment, simply enter the domain in the field site with Facebook Login and set it in the hosts file on your workstation. Here’s how the example of the next step.

Setting the local development environment

If you do not have your own server and want to test the application on your own workstation, configure the hosts file of your ENGINE as example below: localhost

Besides this configuration on the host, you must have at least one configured with Apache and PHP environment. Suggest options WAMP or Xampp .

Creating your application using SDK for PHP

Before we create our php code, let’s first download the facebook sdk for PHP, the link: clicking the Download Zip as shown below:

Creating your application using SDK for PHP

Email extract the zip package into the directory where you will create your application or in place of your own. One named directory will be created facebook-php-sdk-master .

To test your application, create a php file with the content below to your server. Remembering that you must parameterize the appId and secret lines 6 and 7:
(The Secret appId and are obtained in its application within the facebook page as seen above)


// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
 'appId' => '236076976577688',
 'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($user) {
 try {
 // Proceed knowing you have a logged in user who's authenticated.
 $user_profile = $facebook->api('/masud.alam.10420321');
 } catch (FacebookApiException $e) {
 $user = null;

// Login or logout url will be needed depending on current user state.
if ($user) {
 $logoutUrl = $facebook->getLogoutUrl();
} else {
 $statusUrl = $facebook->getLoginStatusUrl();
 $loginUrl = $facebook->getLoginUrl();

// This call will always work since we are fetching public data.
$masud = $facebook->api('/masud.alam.10420321');

<!doctype html>
<html xmlns:fb="">
 body {
 font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
 h1 a {
 text-decoration: none;
 color: #3b5998;
 h1 a:hover {
 text-decoration: underline;

<?php if ($user): ?>
 <a href="<?php echo $logoutUrl; ?>">Logout</a>
 <?php else: ?>
 Check the login status using OAuth 2.0 handled by the PHP SDK:
 <a href="<?php echo $statusUrl; ?>">Check the login status</a>
 Login using OAuth 2.0 handled by the PHP SDK:
 <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
 <?php endif ?>

<h3>PHP Session</h3>
 <pre><?php print_r($_SESSION); ?></pre>

<?php if ($user): ?>
 <img src="<?php echo $user; ?>/picture">

<h3>Your User Object (/me)</h3>
 <pre><?php print_r($user_profile); ?></pre>
 <?php else: ?>
 <strong><em>You are not Connected.</em></strong>
 <?php endif ?>

<h3>Public profile of Masud</h3>
 <img src="">
 <?php echo $masud['name']; ?>

To access your new application, simply use the configured URL on the Site URL . (Remember to be you use any other address other than the configured URL on the Site will not be able to access)

By accessing the app already on your server, the user is redirected to the facebook so he can accept the entry into application. By accepting the application, it will be redirected back. The application will display the user’s photo and public data from it. Show Below:

facebook Access Permision


Congratulations on completing this tutorial. At this point you have created your first application for facebook using PHP SDK . See Sample Output Below:

your first application for facebook using PHP SDK

GoodGood VeryGood 🙂

Create Your First Facebook applications using PHP SDK

2 comments to “Working with Facebook API Part-1:Create Your First Facebook applications using PHP SDK”
  1. This is the right web sitfe for anybody who wishes to find out about this topic.
    You know so mch its almoszt hard to argue with
    you (not that I personally will need to…HaHa). You defiitely put a fresh spin onn a
    topic that’s bee discussed for ages. Excellent stuff, just excellent!

  2. I am facing this error:
    “Fatal error: Call to undefined method Facebook::getLoginStatusUrl() in C:\xampp\htdocs\facebook\ram.php on line 33”

Leave a Reply

Your email address will not be published. Required fields are marked *