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:
After clicking the Register as a Developer link, just follow the directions step by step:
- Accept the terms of membership
- Report details on your user page (you can skip this step if you do not want to enter anything)
- Information that your account is now a developer account
See Figures:
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:
The next screen tells you the basic details of your application:
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)
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:
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)
2 |
require ( 'facebook-php-sdk-master/src/facebook.php' ); |
5 |
$facebook = new Facebook( array ( |
6 |
'appId' => '236076976577688' , |
7 |
'secret' => 'a2db4c087bba39e3ef0b6d11ff018cd6' , |
11 |
$user = $facebook ->getUser(); |
22 |
$user_profile = $facebook ->api( '/masud.alam.10420321' ); |
23 |
} catch (FacebookApiException $e ) { |
31 |
$logoutUrl = $facebook ->getLogoutUrl(); |
33 |
$statusUrl = $facebook ->getLoginStatusUrl(); |
34 |
$loginUrl = $facebook ->getLoginUrl(); |
38 |
$masud = $facebook ->api( '/masud.alam.10420321' ); |
42 |
<html xmlns:fb= "http://www.facebook.com/2008/fbml" > |
44 |
<title>php-sdk</title> |
47 |
font-family: 'Lucida Grande' , Verdana, Arial, sans-serif; |
50 |
text-decoration: none; |
54 |
text-decoration: underline; |
62 |
<a href= "<?php echo $logoutUrl; ?>" >Logout</a> |
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> |
69 |
Login using OAuth 2.0 handled by the PHP SDK: |
70 |
<a href= "<?php echo $loginUrl; ?>" >Login with Facebook</a> |
75 |
<pre><?php print_r( $_SESSION ); ?></pre> |
79 |
<img src= "https://graph.facebook.com/<?php echo $user; ?>/picture" > |
81 |
<h3>Your User Object (/me)</h3> |
82 |
<pre><?php print_r( $user_profile ); ?></pre> |
84 |
<strong><em>You are not Connected.</em></strong> |
87 |
<h3>Public profile of Masud</h3> |
88 |
<img src= "https://graph.facebook.com/masud.alam.10420321/picture" > |
89 |
<?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:
Congratulations
Congratulations on completing this tutorial. At this point you have created your first application for facebook using PHP SDK . See Sample Output Below:
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.