composer require facebook/graph-sdk
This command will download the Facebook sdk and add it to your project.
define('FACEBOOK_APP_ID', 'YOUR_APP_ID');
define('FACEBOOK_APP_SECRET', 'YOUR_APP_SECRET');
define('FACEBOOK_REDIRECT_URI', 'http://localhost/facebook-signin-app/facebook-callback.php');
<?php
require_once 'vendor/autoload.php';
include('config.php');
$fb = new \Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => YOUR_APP_SECRET,
'default_graph_version' => 'v12.0',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl(FACEBOOK_REDIRECT_URI, $permissions);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login with Facebook</title>
</head>
<body>
<h1>Login with Facebook</h1>
<a href="<?= htmlspecialchars($loginUrl) ?>">Login with Facebook</a>
</body>
</html>
<?php
require_once 'vendor/autoload.php';
session_start();
include('config.php');
$fb = new \Facebook\Facebook([
'app_id' => FACEBOOK_APP_ID,
'app_secret' => YOUR_APP_SECRET,
'default_graph_version' => 'v12.0',
]);
$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;
}
if (!isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo 'Access Token
';
var_dump($accessToken->getValue());
// Getting user's info
$oAuth2Client = $fb->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
$tokenMetadata->validateAppId(FACEBOOK_APP_ID);
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "Error getting long-lived access token: " . $e->getMessage() . "
";
exit;
}
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
// Returns a `Facebook\GraphNodes\GraphUser` object
$response = $fb->get('/me?fields=id,name,email', $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
// Store user data in session or database
$_SESSION['id'] = $user['id'];
$_SESSION['name'] = $user['name'];
$_SESSION['email'] = $user['email'];
header('Location: dashboard.php');
exit;
<?php
session_start();
if (!isset($_SESSION['id'])) {
header('Location: login.php');
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
</head>
<body>
<h5>Facebook Account Details</h5>
<p>Facebook ID: <?= htmlspecialchars($_SESSION['id']); ?></p>
<p>Name: <?= htmlspecialchars($_SESSION['name']); ?></p>
<p>Your email: <?= htmlspecialchars($_SESSION['email']); ?></p>
<p><a href="logout.php">Logout</a></p>
</body>
</html>
<?php
session_start();
session_destroy();
header('Location: login.php');
exit();