I was wondering if it is possible to get the name or email of the user inside a Facebook page tab. I've been trying to do this for quite some time now, and can't seem to get it. It was quite easy with the old SDK, but with this one I need the FacebookRequest(), which I can only get after the getSessionFromRedirect(). Now the real problem is when I redirect the user back to Facebook I lose the Session.
The code:
session_start();
define('BASE', 'http://www.example.com/');
define('APP_ID', '123456789');
define('APP_SECRET', 'qwerty');
define('FB_TAB', '//www.facebook.com/example?sk=app_' . APP_ID);
foreach (glob('Facebook/*.php') as $filename) { require_once $filename; }
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
if(isset($_SESSION['fb_session']) && !empty($_SESSION['fb_session']))
{
// if already logged print user info
$user_profile = (new FacebookRequest(
$_SESSION['fb_session'], 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo 'Name: ' . $user_profile->getName(); die;
}
else
{
// otherwise redirect user to login
try {
FacebookSession::setDefaultApplication(APP_ID, APP_SECRET);
$helper = new FacebookRedirectLoginHelper(BASE);
$session = $helper->getSessionFromRedirect();
if($session)
{
// if has token save into session to use later
$_SESSION['fb_session'] = $session;
header('Location: ' . FB_TAB);
}
else
{
// get Facebook login url
echo '<script>top.location.href = "'.$helper->getLoginUrl().'";</script>'; die;
}
}
catch(FacebookRequestException $e) { var_dump($e); }
catch(\Exception $e) { var_dump($e); }
}
Explaining by steps:
As we enter the Facebook page tab, I check for the $_SESSION. Because it's the first time I can't find it, so I redirect the user to the Facebook login (getLoginUrl).
As we exit the page tab I get the object through (getSessionFromRedirect) and save it in the $_SESSION. So far so good. The problem resides here, after I save the $_SESSION I redirect the user back to the Facebook page tab (This is where it get's tricky).
When the user enters the page tab, we're suppose to have the object inside the $_SESSION, but it is gone.
So the questions is... Using the SDK 4, is there any way to solve this? or does anyone know a way to get the user information without getting this FacebookSession?
Thanks :)