2013-03-24 95 views
3

您是否有過如何確認嘗試訪問您的應用內容的人是否是使用PHP的特定Facebook羣組成員的示例?我已經閱讀了開發人員文檔,它所做的一切讓我更加困惑。我見過類似這個帖子,但他們只顯示我如何打印成員列表...檢查用戶是否是特定Facebook羣組的成員

我需要知道如何獲取列表,看看用戶試圖訪問該應用程序是一個成員我可以:a允許訪問或b:拒絕訪問並將用戶重定向到組頁面,以便他們可以請求成員資格...

回答

4

是的,它很簡單,你只需要知道您要執行的組的ID,並請求範圍user_groups權限,以便您可以查詢用戶擁有的組。

隨着PHP SDK,你可以這樣做:

<?php 
    // Remember to copy files from the SDK's src/ directory to a 
    // directory in your application on the server, such as php-sdk/ 
    require_once('php-sdk/facebook.php'); 

    $config = array(
    'appId' => 'YOUR_APP_ID', 
    'secret' => 'YOUR_APP_SECRET', 
); 

    $facebook = new Facebook($config); 
    $user_id = $facebook->getUser(); 
if($user_id) { 

     // We have a user ID, so probably a logged in user. 
     // If not, we'll get an exception, which we handle below. 
     try { 

     $user_profile = $facebook->api('/me?fields=id,name,groups','GET'); 
     $user_belongs_to_group= FALSE; 
     foreach($user_profile['groups']['data'] as $group){ 
      if($group['id']==YOUR_GROUP_ID) 
      $user_belongs_to_group=TRUE; 
     } 
     if($user_belongs_to_group){ 
     //Alright! the user belongs to the group 
     } 
     else{ 
      //Oh snap you don't belong here, but you can if want to 
     } 

     } catch(FacebookApiException $e) { 
     // If the user is logged out, you can have a 
     // user ID even though the access token is invalid. 
     // In this case, we'll get an exception, so we'll 
     // just ask the user to login again here. 
     $login_url = $facebook->getLoginUrl(); 
     echo 'Please <a href="' . $login_url . '">login.</a>'; 
     error_log($e->getType()); 
     error_log($e->getMessage()); 
     } 
    } else { 

     // No user, print a link for the user to login 
     $login_url = $facebook->getLoginUrl(); 
     echo 'Please <a href="' . $login_url . '">login.</a>'; 

     } 

?> 

一些這方面的代碼,我從Facebook的抓住的,你可以看到返回的數據時,查詢組在這裏:https://developers.facebook.com/tools/explorer

新聞名稱後的加號進入連接,選擇組並按發送

0

您處於catch-22狀態,您將需要用戶通過user_groups權限授予他的組權限。

相關問題