Introduction

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 linkhttps://developers.facebook.com 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 https://developers.facebook.com/apps/ 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

Remarks:

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 :/ / developers.facebook.com / apps and click the Edit Application button)

Configuring the newly created application

Remarks:

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:

127.0.0.1 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:https://github.com/facebook/facebook-php-sdk 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)

1 <?php
2 require('facebook-php-sdk-master/src/facebook.php');
3  
4 // Create our Application instance (replace this with your appId and secret).
5 $facebook new Facebook(array(
6  'appId' => '236076976577688',
7  'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6',
8 ));
9  
10 // Get User ID
11 $user $facebook->getUser();
12  
13 // We may or may not have this data based on whether the user is logged in.
14 //
15 // If we have a $user id here, it means we know the user is logged into
16 // Facebook, but we don't know if the access token is valid. An access
17 // token is invalid if the user logged out of Facebook.
18  
19 if ($user) {
20  try {
21  // Proceed knowing you have a logged in user who's authenticated.
22  $user_profile $facebook->api('/masud.alam.10420321');
23  } catch (FacebookApiException $e) {
24  error_log($e);
25  $user = null;
26  }
27 }
28  
29 // Login or logout url will be needed depending on current user state.
30 if ($user) {
31  $logoutUrl $facebook->getLogoutUrl();
32 else {
33  $statusUrl $facebook->getLoginStatusUrl();
34  $loginUrl $facebook->getLoginUrl();
35 }
36  
37 // This call will always work since we are fetching public data.
38 $masud $facebook->api('/masud.alam.10420321');
39  
40 ?>
41 <!doctype html>
42 <html xmlns:fb="http://www.facebook.com/2008/fbml">
43  <head>
44  <title>php-sdk</title>
45  <style>
46  body {
47  font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
48  }
49  h1 a {
50  text-decoration: none;
51  color: #3b5998;
52  }
53  h1 a:hover {
54  text-decoration: underline;
55  }
56  </style>
57  </head>
58  <body>
59  <h1>php-sdk</h1>
60  
61 <?php if ($user): ?>
62  <a href="<?php echo $logoutUrl; ?>">Logout</a>
63  <?php else: ?>
64  <div>
65  Check the login status using OAuth 2.0 handled by the PHP SDK:
66  <a href="<?php echo $statusUrl; ?>">Check the login status</a>
67  </div>
68  <div>
69  Login using OAuth 2.0 handled by the PHP SDK:
70  <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
71  </div>
72  <?php endif ?>
73  
74 <h3>PHP Session</h3>
75  <pre><?php print_r($_SESSION); ?></pre>
76  
77 <?php if ($user): ?>
78  <h3>You</h3>
79  <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
80  
81 <h3>Your User Object (/me)</h3>
82  <pre><?php print_r($user_profile); ?></pre>
83  <?php else: ?>
84  <strong><em>You are not Connected.</em></strong>
85  <?php endif ?>
86  
87 <h3>Public profile of Masud</h3>
88  <img src="https://graph.facebook.com/masud.alam.10420321/picture">
89  <?php echo $masud['name']; ?>
90  </body>
91 </html>

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

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

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.