0
Good evening, I’m implementing Facebook login in my application.
But the line below always returns NULL
$session = $helper->getSessionFromRedirect();
Someone knows what could be wrong?
Below the full code:
<?php
session_start();
// added in v4.0.0
require_once './autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
// init app with app id and secret
FacebookSession::setDefaultApplication('{id}', '{secret}');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://localhost/PedeAqui');
try {
$session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
var_dump($ex);
} catch (Exception $ex) {
var_dump($ex);
}
// see if we have a session
if (isset($session)) {
// graph api request for user data
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
// get response
$request = new FacebookRequest($session, 'GET', '/me?fields=email,first_name,last_name,gender,name,id');
$response = $request->execute();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables ----- */
$_SESSION['cod_usuario'] = $fbid;
$_SESSION['usuario'] = $fbfullname;
$_SESSION['email'] = $femail;
$_SESSION['tipo'] = 2;
/* ---- header location after session ---- */
header("Location: http://localhost/PedeAqui/index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: " . $loginUrl);
}
?>
I imagine the App Domains and the Site URL are set up ok in the Dashboard of your App, right?
– brasofilo
@brasofilo, yes, are configured.
– Kelvin Eger