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.