Tutorial Facebook SDK V4 for PHP for Beginners

Tutorial facebook SDK Version 4.0.0 for PHP . Facebook provides a free SDK for developers to develop applications to access Facebook Graph API. Facebook provides a web server for each programming language. In this case, I use the PHP SDK for communication with facebook API. In my opinion, the previous version of the PHP SDK version 3.2.3, it is relatively easier to use than using the php version to 4.0.0. For more information about the tutorial facebook SDK for php can be read directly at the source

In this article I will share the basics of using Facebook PHP SDK Version 4.0.0 step by step. The things to be learned like how to create a facebook app, facebook How to communicate with the API, a simple example of the use of facebook app.

Please know, Facebook has tightened the requirement for developers in the development of facebook app. In fact you need to do the filing to be able to access some APIs that are sensitive like the app details, manage notifications, manage page, or the most frequently used / searched looking for website developers that “publish action” and “publish page“. The terms of the filing are considered easy if you actually make an application with a positive goal.

How to create your facebook app

1. Login to your facebook account developer here
2. Register new app as developer
How to create facebook app with php - Tutorial facebook sdk v4.0.0 php3. Verify your new facebook app with your phone number and you will recieve confirmation code
how to verify facebook app with free - Tutorial facebook sdk
4. Give your application name and click create application id button
how to use facebook sdk 4 with php
5. Congrat, you will get app id and app secret number from your facebook app

The first step for creating facebook App is finished, Next you need to complete your application detail , so you can get more access to facebook API. Please complete your detail at your facebook app dashboard

how to create facebook apps mobile

Getting Started tutorial using facebook sdk 4.0.0 with PHP begginer
1. Download Facebook PHP SDK 4.0.0 from download facebook php sdk 4.0.0 (The Facebook SDK for PHP v4 requires PHP 5.4 or greater.)
2. Extract zip file and copy Facebook folder from src folder to your root folder of your site

copy facebook sdk to your root folder
3. create php file as index.php

Script how to get started using the PHP SDK 4.0 for facebook app

<?php
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once('Facebook/FacebookPermissionException.php');
require_once('Facebook/FacebookAuthorizationException.php');

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookResponse;
use Facebook\FacebookPermissionException;
use Facebook\GraphUser;
use Facebook\FacebookAuthorizationException;

FacebookSession::setDefaultApplication('your-app-id', 'your-secret-id');
$helper = new FacebookRedirectLoginHelper('http://localhost/facebookapp/index.php' );
try {
  $session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
  echo $e->getMessage();
} catch( Exception $ex ) {
  echo $e->getMessage();
}

if ( $session) {
  print_r($session);
  try {
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request -> execute();
    $me = $response-> getGraphObject();
    echo 'My facebook name is : '.$me->getProperty('name');

  } catch(FacebookRequestException $e) {

    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();

  }
} else {
  $auth_url = $helper->getLoginUrl();
 echo '<a href="' . $auth_url . '">Login</a>';
}

Check this script to your server! , at my example : http://localhost/facebookapp/index/php

You will get request application to your facebook account like image below
facebook app permission

Choose “Okay” to accepted, and then you will redirect to your redirect link you have registered on your facebook account develepor.

facebook app request name

You can change $me->getProperty(‘name’) with another facebook property like id, photos, location, etc, read the reference here

Yes, your first tutorial facebook sdk V4 is finish. How about auto post to your facebook timeline or page ? Copy this script below


if ( $session) {
 try {
 $postrequest = new FacebookRequest($session, 'POST', '/me/feed',array('message' => 'my first timeline using facebook app'));
 $postresponse = $postrequest -> execute();
 $postme = $postresponse-> getGraphObject();
 echo $postme->getProperty('id');

} catch(FacebookRequestException $e) {

echo "Exception occured, code: " . $e->getCode();
 echo " with message: " . $e->getMessage();

}
} else {
  $auth_url = $helper->getLoginUrl();
 echo '<a href="' . $auth_url . '">Login</a>';
}

I know, you will get error message something like this

Exception occured, code: 200 with message: (#200) The user hasn’t authorized the application to perform this action

how to resolve facebook app error code 200

How to post article to your facebook page and resolve error message “Exception occured, code: 200 with message: (#200) The user hasn’t authorized the application to perform this action” i will explain in the next article.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: