0
<?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()方法将返回零。

发生这种情况有原因吗?

4

1 回答 1

2

Facebook PHP SDK路径、、appIdsecret替换redirect_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();
}
于 2013-07-20T12:40:45.520 回答