আগামী ৩০ অক্টোবর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML,CSS,Bootstrap,PHP,MySQl, AJAX and JQUERY কোর্সের ৮৬ তম ব্যাচ। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722817591, Email : [email protected] কোর্সের সিলেবাস এর জন্য এখানে ক্লিক করুন ।

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

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:

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

Configuring the newly created application

Remarks:

  • 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 http://www.minha-app-facebook.com.br/meuapp.php , please just http://www.minha-app-facebook.com.br .
  • 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, pleasehttp://www.minha-app-facebook.com.br:8080
  • 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:

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)

<?php
require('facebook-php-sdk-master/src/facebook.php');

// 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) {
 error_log($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="http://www.facebook.com/2008/fbml">
 <head>
 <title>php-sdk</title>
 <style>
 body {
 font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
 }
 h1 a {
 text-decoration: none;
 color: #3b5998;
 }
 h1 a:hover {
 text-decoration: underline;
 }
 </style>
 </head>
 <body>
 <h1>php-sdk</h1>

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

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

<?php if ($user): ?>
 <h3>You</h3>
 <img src="https://graph.facebook.com/<?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="https://graph.facebook.com/masud.alam.10420321/picture">
 <?php echo $masud['name']; ?>
 </body>
</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 graduated in 2009 with a bachelor's degree in Engineering from State University Of Bangladesh, I'm also 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, CIDA and MAX Group where I worked on ERP software and web development., but now i'm a co-founder and Chief Executive Officer and Managing Director of TechBeeo Software Consultancy Services 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.
2 comments on “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 *