为什么我不能重定向到Facebook的getLoginUrl()?

时间:2013-07-20 09:36:34

标签: facebook facebook-php-sdk

<?php
$fb = new Facebook(array(
    'appId' => $fb_config['app_id'],
    'secret' => $fb_config['secret'],
));

$params = array(
    'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
    'redirect_uri' => 'http://myurl/facebook_connect',
);

$fb_login_url = $fb->getLoginUrl($params);

$this->redirect($fb_login_url);

$user = $fb->getUser();

print_r($user); // returns 0
?>

在上面的示例中,如果我重定向到getLoginUrl结果,getUser()方法将返回零。

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:2)

使用您的应用设置替换Facebook PHP SDK路径,appIdsecretredirect_uri,然后尝试:

require_once dirname(__FILE__) . '/facebook/facebook.php'; // You need Facebook PHP SDK
$facebook = new Facebook(array(
    'appId' => $fb_config['app_id'],  // Your Facebook app id
    'secret' => $fb_config['secret'], // Your Facebook app secret
    'cookie' => true
));

$fb_user_id = $facebook->getUser();
if ($fb_user_id) {
    try {
        $fb_user_profile = $facebook->api($fb_user_id);
        var_dump($fb_user_profile);
    } catch (FacebookApiException $e) {
        error_log($e);
    }
} else {
    $params = array(
        'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
        'redirect_uri' => 'http://myurl/facebook_connect', // Replace with your app url
    );
    $facebook_login_url = $facebook->getLoginUrl($params);
    echo '<script>top.location="' . $facebook_login_url . '";</script>';
    exit();
}