2012-02-02 83 views
1

我正在創建一個與Facebook集成的簡單概念驗證Web應用程序。基本功能是它需要顯示有關用戶的基本信息,以及他們的一些朋友+朋友個人資料圖片。它似乎工作,減去註銷 - 當用戶點擊我的web應用程序,這是通過Facebook PHP-SDK生成的註銷鏈接,頁面看起來刷新 - 用戶顯然仍然登錄到我的web應用程序。但是,他們從Facebook登出。任何想法爲什麼我看到這種行爲?我的代碼如下:使用Facebook PHP-SDK,我如何確定用戶是否已註銷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> 

回答

0

如果您使用的是PHP SDK版本3,您還需要調用destroySession功能,因爲它在一個會話保存用戶數據。

+0

雖然這可行,但它也可以防止用戶登錄;;) – rybosome 2012-02-02 16:41:59

+0

將註銷URL指向單獨的腳本,例如, logout.php然後運行destroySession函數,然後將您重定向到getLogoutUrl。 – fire 2012-02-02 16:47:21