May 22, 2014 9:30 am

How to Login with Facebook using SDK 4.0 in PHP

As you all know that Facebook recently published SDK 4.0 so today I am going to give you a tutorial on how to login with Facebook using SDK 4.0. We have previously published article on Graph API login and now we are moving towards new SDK 4.0, this SDK only can run on PHP 5.4 or higher so please make sure that you have PHP updated version installed.
How to Login with Facebook using SDK 4.0 in PHP

Updated SDK 100% Working!!

NOTE: You must need PHP version >= 5.4 else it will not work shows errors only.

This new SDK is more easy to configure and easy to call methods we are giving you a very simple and easy code for login application as we already published a tutorial on Graph login so this is the same tutorial with Facebook SDK 4.0.

Working code:

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// Initialize application by Application ID and Secret
// Login Healper with reditect URI
$helper = new FacebookRedirectLoginHelper( '' );
try {
  $session = $helper->getSessionFromRedirect();
catch( FacebookRequestException $ex ) {
  // Exception
catch( Exception $ex ) {
  // When validation fails or other local issues
// Checking Session
  // Request for user data
  $request = new FacebookRequest( $session, 'GET', '/me' );
  $response = $request->execute();
  // Responce
  $data = $response->getGraphObject();
  // Print data
  echo  print_r( $data, 1 );
  // Login URL if session not found
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';

Above code will get your Application ID and Secret to initialize your application and check if session available then show current users’s information else show you a login link using that you can login and redirected back to your website with user’s information.

In the above whole code we are mission one thing that how can we get permissions in scope parameter I have faced same for first time but found its how do we send scope in it here is the solution $helper->getLoginUrl(array(’email’,’manage_pages’)). We have to send permission in an array and put that array in helper getLoginUrl function and it will send them in url.

Scope (Permission)

// Login URL if session not found
echo '<a href="' . $helper->getLoginUrl(array('email','manage_pages') . '">Login</a>';

You can download SDK from here.

[wpdm_file id=103]

I am not giving any Demo for this tutorial because it required PHP 5.4 or greater and my server’s PHP version is 5.3, I have tested that code on my local development machine and its working perfect and hope it will work fine at your end as well.

Please don’t forget  to give us your feedback and suggestion.

There is many things in that new Facebook SDK 4.0 and we are working on them and publish new tutorials.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.

Tutorial Categories:

23 responses to “How to Login with Facebook using SDK 4.0 in PHP”

  1. Zura Labadze says:

    it’s ok. i did this yet . but facebook tab page does’t get user info and can you help me ?

  2. carlos villablanca says:

    do you can upload a video about this tutorial? Please 🙂

  3. emmanuel says:

    do you knwo how to login and post status but without any login interface (in a batch process – server-side) ?

  4. Bill says:

    I receive this error, and am unable to print the object on the response.

    PHP Fatal error: Class ‘FacebookFacebookCurlHttpClient’ not found in /home2/cardeman/public_html/Facebook/FacebookRequest.php on line 154

  5. Kimberley says:

    I found this tutorial very helpful. I am using the Composer autoload instead of the separate requires, but I can connect and display some profile data. The Facebook SDK documentation leaves a lot of gaps for people new to developing with php, so thank you for showing what else to put in.

  6. Nikunj Vagadiya says:

    how can i make get album list
    on second page instead of same page

  7. Ajinkya Dub says:

    Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or ‘(‘ in /home/movecart/public_html/bfbuilder/SDK/index.php on line 12

    • Gnik Nalu says:

      Update your PHP version and it will resolve the ” unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING” error

  8. Nilson Rodrigues says:

    Fatal error: Class ‘FacebookFacebookCurlHttpClient’ not found in /home/u478284193/public_html/4/Facebook/FacebookRequest.php on line 154

  9. Alaa Eltayeb says:

    i get

    500 (Internal Server) Error at this line

    $request = new FacebookRequest( $session, ‘GET’, ‘/me’ ); plz advice thx

  10. sfd;l says:

    doesnot work

  11. Rui Costa says:

    I’ve a problem with this script. The example is working, with my app facebook credentials, but, when i refresh my webpage, i need to click login, like if the session expires. Any tip?

  12. naqqash says:

    login successful with facebook but not showing user info again showing login link please help

    and the error is from my error log is

    Fatal error: Class ‘FacebookFacebookCurlHttpClient’ not found in /home/itclient/public_html/Facebook/FacebookRequest.php on line 154

  13. Developer says:

    Thanks for the tutorial

Leave a Reply

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