连接到Facebook时我得到一个空的用户

时间:2011-01-30 08:23:12

标签: php facebook

我是Facebook新手,我正在尝试编写我的第一个应用程序。

我尝试了以下基本代码,然后进入了“发生错误”部分 据我所知,我得到的是会议而不是用户详细信息...... 你可以帮我解决出了什么问题吗?非常感谢:

<?php

require 'facebook.php';

//create application instance
$facebook = new Facebook(array(
'appId' => 'XXXXXX',
'secret' => 'XXXXXX',
'cookie' => true,
));

$session = $facebook->getSession();

if (!empty($session))
{
    try {
        $uid = $facebook->getUser();
    $user = $facebook->api('/me');
    } catch (Exception $e){}

    if (!empty($user)){
        if($_GET['installed']=='1'){
                header("Location: http://apps.facebook.com/lbsresearch/");
    }
    echo "Hello user";
    } 
    else {
    die ("An error occured");
    }
}

else
{
$url = $facebook->getLoginUrl();
echo "<a href='".$url."'>Click here</a> to add this Facebook application";    
}

?>

1 个答案:

答案 0 :(得分:1)

在你让用户有机会登录之前,好像你正在死去。 整个Facebook检查对我来说也很混乱,我可以帮助你使用代码和我使用的一些评论(写了一次,让它运行并在任何地方重复使用;)) CALLBACK_URL是您的服务器上的应用程序。

$facebook = new Facebook(array(
    'appId' => APP_ID,
    'secret' => APP_SECRET,
    'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// some random permissions
$permissions = 'publish_stream, create_event, rsvp_event, manage_pages, ads_management, user_photos';

// Session based API call.
if ($session) {
    try {
        $uid = $facebook->getUser();
        $me = $facebook->api('/' . $uid);

    } catch (FacebookApiException $e) {
        var_dump($e);
    }
}
// here, check if the user has the application added
if ($me == null) {
    $loginUrl = $facebook->getLoginUrl(array(
        'canvas' => 1,
        'fbconnect' => 0,
        'req_perms' => $permissions,
        'next' => CALLBACK_URL . '?installed=1', // will be redirected to this URL after pressing "allow"
        'cancel_url' => CALLBACK_URL . '?cancel', // will be redirected here if app is not allowed
    )
    ); // end of array
    // redirect the user to the $loginUrl, you can place a button if you want
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}

我希望它对你有用:)