繁体   English   中英

使用Facebook PHP-SDK,如何确定用户是否已注销Facebook?

[英]Using Facebook PHP-SDK, how can I determine if a user has logged out of Facebook?

我正在创建一个与Facebook集成的简单概念验证网络应用。 基本功能是它需要显示有关用户的基本信息,以及一些他们的朋友和朋友个人资料图片。 一切似乎都有效,减去了注销-当用户单击通过Facebook PHP-SDK生成的我的Web应用程序上的注销链接时,页面似乎只是刷新了-用户显然仍登录到我的Web应用程序。 但是,他们已从Facebook正确注销。 知道为什么我会看到这种行为吗? 我的代码如下:

<?php

require 'sdk/facebook.php';

$facebook = new Facebook(
    array(
        'appId' => 'xxx' //actual app ID and secret go here,
        'secret' => 'xxx'
    )
);

##
## Instantiate user object if a logged in user exists
if ($user = $facebook->getUser()) {
    try {
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        $user = NULL;
    }
}

##
## Set login/logout url
$url = $user ? $facebook->getLogoutUrl() : $facebook->getLoginUrl();

##
## Instantiate friend object data
if ($user) {
    try {
        $friend_api_data = $facebook->api('/me/friends');
        $friends = $friend_api_data['data'];
        // Shuffle friends
        shuffle($friends);
        $shuffled_friends = array();
        for ($i = 0; $i < count($friends) && $i < 6; $i++) {
            $shuffled_friends[] = $friends[$i];
        }   
    } catch (FacebookApiException $e) {

    }

}

?>
<!doctype html>
<html>
<head>
    <title>Test</title>
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div class="container">
        <h1>Facebook Integration</h1>

        <!-- Login/Logout URL -->
        <a href="<?= $url ?>"><?= $user ? 'Logout' : 'Login' ?></a>

        <!-- Current User Data -->
        <?php if ($user): ?>
            <h2>Hi, <?= $user_profile['name'] ?></h2>
            <img src="https://graph.facebook.com/<?= $user ?>/picture" />

            <pre>
                <?= print_r($user_profile, TRUE) ?>
            </pre>
        <?php endif; ?>

        <!-- Friend Data -->
        <?php if($shuffled_friends): ?>
            <h2>Your Friends</h2>
            <ul>
                <?php foreach($shuffled_friends as $f): ?>
                    <li>
                        <a href="http://www.facebook.com/<?= $f['id'] ?>">
                            <img src="https://graph.facebook.com/<?= $f['id'] ?>/picture" />
                            <?= $f['name'] ?>
                        </a>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </div>  
</body>
</html>

如果您使用的是PHP SDK的第3版,则还需要调用destroySession函数,因为它将用户数据保存在会话中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM