drby30217 2014-09-30 14:00
浏览 42

使用Facebook SDK 4在标签内获取用户信息

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:

  1. 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).

  2. 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).

  3. 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 :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数