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:

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)

You can download SDK from here.

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:
  • Zura Labadze

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

  • carlos villablanca

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

  • emmanuel

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

  • Bill

    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

  • Kimberley

    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.

  • Nikunj Vagadiya

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

  • Ajinkya Dub

    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

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

  • Nilson Rodrigues

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

    • huzoorbux

      Check your script include files path.

  • Alaa Eltayeb

    i get

    500 (Internal Server) Error at this line

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

    • huzoorbux

      Turn on errors and test it see what is the exact error on your script

  • sfd;l

    doesnot work

  • 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?

    • huzoorbux

      Store access token in session and reuse it on page reload

      • I’m trying to do that, but i can’t get access token. Can you help me, please?

        • huzoorbux

          What response you are getting from Facebook when trying to token post error

  • naqqash

    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

    • verify files paths.

      • naqqash

        sir everything is according to your tutorial in spite of that if you have any suggestions please let me know so that i could change it

      • naqqash

        please it is requested to you that do something in this regard because i have stuck on this task for one week please i’ll be very very thankful to you for this kindness

  • Developer

    Thanks for the tutorial