Hai,
I want to integrate login with Facebook in my website. and i created APP id and Secret Key, but i am not getting the source code for the integration…!
any suggestion please…?
thanks,
Darshan
Hai,
I want to integrate login with Facebook in my website. and i created APP id and Secret Key, but i am not getting the source code for the integration…!
any suggestion please…?
thanks,
Darshan
I think you need the Facebook SDK for PHP
A nice and recent tut could be found here (just skip the server configuration steps).
One comment: Facebook requires you to use a domain protected by SSL.
Let us know if you have any problems…
@frankbeen thanks for your suggestion i used now Facebook SDK but now after login with face book i waht to redirect to particular custom page but right now loading to face book page…
where should i change the redirection link…?
**<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set('memory_limit', '-1');
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-sdk/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
$appId = '****************'; //Facebook App ID
$appSecret = '**************************'; //Facebook App Secret
$redirectURL = 'https://twitter.com/login'; //Callback URL
$fbPermissions = array('email'); //Optional permissions
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.9',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
// Already login
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// Redirect the user back to the same page if url has "code" parameter in query string
if (isset($_GET['code'])) {
// Getting user facebook profile info
try {
$profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
$fbUserProfile = $profileRequest->getGraphNode()->asArray();
// Here you can redirect to your Home Page.
echo "<pre/>";
print_r($fbUserProfile);
} catch (FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// Redirect user back to app login page
header("Location: ./");
exit;
} catch (FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
} else {
// Get login url
$loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
header("Location: " . $loginURL);
}
} catch (FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>**
thanks,
Darshan
I am missing a part from the tutorial:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
ini_set('memory_limit', '-1');
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-sdk/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
$appId = '****************'; //Facebook App ID
$appSecret = '**************************'; //Facebook App Secret
$redirectURL = 'https://twitter.com/login'; //Callback URL
$fbPermissions = array('email'); //Optional permissions
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.9',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$accessToken = $helper->getAccessToken();
}
} catch (FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// Redirect the user back to the same page if url has "code" parameter in query string
if (isset($_GET['code'])) {
header('Location: whateverpageyoulike.php'); // CHANGE HERE
}
// Getting user facebook profile info
try {
$profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
$fbUserProfile = $profileRequest->getGraphNode()->asArray();
// Here you can redirect to your Home Page.
print_r($fbUserProfile);
} catch (FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// Redirect user back to app login page
header("Location: ./");
exit;
} catch (FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
} else {
// Get login url
$loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
header("Location: " . $loginURL);
}
?>*
Instead of profile.php you could direct to anywhere you like.
you are saying Here…!
try {
$profileRequest = $fb->get(’/me?fields=name,first_name,last_name,email,link,gender,locale,picture’);
$fbUserProfile = $profileRequest->getGraphNode()->asArray();
// Here you can redirect to your Home Page.
echo “”;
print_r($fbUserProfile);
}
edited my previous post to your complete page with some little changes.
Just now i replaced what you suggested code…,
but it is loading in face book page only…(Facebook - Log In or Sign Up)
i want to load after login successful admindashboard.php file…
can u help me please…?
I got the solution…,
thank you…,
I am not able to get the below values… ! it is showing blank page…
<?php
session_start();
?>
<head>
<title>Login with Facebook</title>
<link
href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
rel = "stylesheet">
</head>
<body>
<?php if($_SESSION['fb_id']) {?>
<div class = "container">
<div class = "jumbotron">
<h1>Hello <?php echo $_SESSION['fb_name']; ?></h1>
<p>Welcome to Cloudways</p>
</div>
<ul class = "nav nav-list">
<h4>Image</h4>
<li><?php echo $_SESSION['fb_pic']?></li>
<h4>Facebook ID</h4>
<li><?php echo $_SESSION['fb_id']; ?></li>
<h4>Facebook fullname</h4>
<li><?php echo $_SESSION['fb_name']; ?></li>
<h4>Facebook Email</h4>
<li><?php echo $_SESSION['fb_email']; ?></li>
</ul>
</div>
<?php } ?>
any suggestion please…
thanks,
Darshan
check the source code, mostly ctrl+u in the browser. if there’s nothing to see, you have to look into the error.log of your webserver.
Got it …
thanks you