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.
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:
<?php session_start(); 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 FacebookSession::setDefaultApplication('ApplicationID','ApplicationSecret'); // Login Healper with reditect URI $helper = new FacebookRedirectLoginHelper( 'https://www.phpgang.com' ); try { $session = $helper->getSessionFromRedirect(); } catch( FacebookRequestException $ex ) { // Exception } catch( Exception $ex ) { // When validation fails or other local issues } // Checking Session if(isset($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 ); } else { // 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.
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.
Tutorial Categories:
it’s ok. i did this yet . but facebook tab page does’t get user info and can you help me ?
do you can upload a video about this tutorial? Please 🙂
do you knwo how to login and post status but without any login interface (in a batch process – server-side) ?
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
The internals of the SDK are being changed up quite a bit all the time so it’s much better to implement autoloading instead of doing a bunch of `require`’s at the top of the script.
It’s best to install the SDK with composer: https://developers.facebook.com/docs/php/gettingstarted/4.0.0
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.
how can i make get album list
on second page instead of same page
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
Update your PHP version and it will resolve the ” unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING” error
Fatal error: Class ‘FacebookFacebookCurlHttpClient’ not found in /home/u478284193/public_html/4/Facebook/FacebookRequest.php on line 154
Check your script include files path.
i get
500 (Internal Server) Error at this line
$request = new FacebookRequest( $session, ‘GET’, ‘/me’ ); plz advice thx
Turn on errors and test it see what is the exact error on your script
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?
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?
What response you are getting from Facebook when trying to token post error
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.
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
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
Thanks for the tutorial