I am writing on latest SDK v5 how to login with Facebook using PHP SDK v5 which is the latest version and I suggest you all to update your old Facebook login integration to the latest version.
Let’s Start
Make New Application on Facebook:
1. Visit https://developers.facebook.com/apps and click + Create New App.
Click on website.
Add application name and click Create New Facebook App ID.
Select category and click on Create App ID. On next window click Skip Quick Start.
It will take you to the app dashboard now select settings and click on +Add Platform. Select website in add platform popup.
Add your website link and fill form and save like below image.
By default your app enabled for sandbox mode you need to change it to live click on Status & Review.
That’s it App settings done now come to the coding.
First of all download SDK from here or you can download our demo code which contain SDK as well.
Now create a file index.php and add a facebook login button link to fblogin.php page and create a callback.php page to show information after authenticate.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Login With Facebook</title>
</head>
<body>
<a href="fblogin.php"><img src="login-button.png" alt="Facebook Login Button"></a>
</body>
</html>
It will show a login with Facebook image.
fblogin.php
<?php
session_start();
require_once( 'Facebook/autoload.php' );
$fb = new Facebook\Facebook([
'app_id' => 'ENTER-YOUR-APP-ID',
'app_secret' => 'ENTER-YOUR-APP-SECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions for more permission you need to send your application for review
$loginUrl = $helper->getLoginUrl('http://demo.phpgang.com/facebook-login-sdk-v5/callback.php', $permissions);
header("location: ".$loginUrl);
?>
This page verify your Facebook application credentials and redirect you to facebook.com for login. You need to change your application id and application secret.
Once you allow all permission it will redirect to callback.php page and show your user’s data.
callback.php
<?php
session_start();
require_once( 'Facebook/autoload.php' );
require_once( 'db.php' );
$fb = new Facebook\Facebook([
'app_id' => 'ENTER-YOUR-APP-ID',
'app_secret' => 'ENTER-YOUR-APP-SECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=id,name,email,first_name,last_name', $accessToken->getValue());
// print_r($response);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'ERROR: Graph ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'ERROR: validation fails ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
//print_r($me);
echo "Full Name: ".$me->getProperty('name')."<br>";
echo "First Name: ".$me->getProperty('first_name')."<br>";
echo "Last Name: ".$me->getProperty('last_name')."<br>";
echo "Email: ".$me->getProperty('email');
echo "Facebook ID: <a href='https://www.facebook.com/".$me->getProperty('id')."' target='_blank'>".$me->getProperty('id')."</a>";
?>
That’s it for codding end now you can integrate it and run latest version of Facebook login.