如何通过sdk v5获取用户在Facebook上加入的群组列表?

时间:2015-08-31 19:13:26

标签: facebook sdk

我希望使用facebook sdk v5获取我加入的所有群组名称。

我已经创建了我创建的组名,但是如何获得我加入的组名。我正在使用下面的代码。

$fb->setDefaultAccessToken($accessToken);


 $res = $fb->get( '/me/groups' );

$feed = $res->getDecodedBody();

var_dump( $feed );

它给了我创建的用户组,我不想要。

我怎样才能让它发挥作用?

2 个答案:

答案 0 :(得分:1)

无法再获取该信息,您只能使用user_managed_groups权限和/me/groups端点获取您管理的群组。您可以在changelog中详细了解相关内容。

答案 1 :(得分:0)

$app_id             = 'yourid';  //localhost
$app_secret         = 'yoursecret';
$required_scope     = 'public_profile, publish_actions, user_groups, email'; //Permissions required
$redirect_url       = 'http://yoururl'; //FB redirects to this page with a code
require_once __DIR__ . "/facebook-php-sdk-v4-4.0-dev/autoload.php"; //include autoload from SDK folder

//import required class to the current scope
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;

FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);


try {
  $session = $helper->getSessionFromRedirect();

} catch(FacebookRequestException $ex) {
    die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
    die(" Error : " . $ex->getMessage());
}



//if user wants to log out
if(isset($_GET["log-out"]) && $_GET["log-out"]==1){
    unset($_SESSION["fb_user_details"]);
    unset($_SESSION["email"]);

    echo '<meta http-equiv="refresh" content="1; url=/postlogin.php">';
}



//Test normal login / logout with session

if ($session){ //if we have the FB session


    //get user data
    $user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
    $user_groups = (new FacebookRequest($session, 'GET', '/me/groups'))->execute()->getGraphObject(GraphUser::className());



  //do a regular session here for access token.
$accessToken = $session->getAccessToken();

$longLivedAccessToken = $accessToken->extend();


    $_SESSION["fb_user_groups"] = $user_groups->asArray(); 
    $group_id = ( isset( $_SESSION["fb_user_groups"]["id"] ) )? $_SESSION["fb_user_groups"]["id"] : "";
    $group_name = ( isset( $_SESSION["fb_user_groups"]["name"] ) )? $_SESSION["fb_user_groups"]["name"] : "";


    //save session var as array
    $_SESSION["fb_user_details"] = $user_profile->asArray(); 


    $user_id = ( isset( $_SESSION["fb_user_details"]["id"] ) )? $_SESSION["fb_user_details"]["id"] : "";
    $user_name = ( isset( $_SESSION["fb_user_details"]["name"] ) )? $_SESSION["fb_user_details"]["name"] : "";
    $user_email = ( isset( $_SESSION["fb_user_details"]["email"] ) )? $_SESSION["fb_user_details"]["email"] : "";

        if(isset($_SESSION["fb_user_details"]))
    {



    print '<pre>';
        print_r($_SESSION["fb_user_details"]);
        print '</pre>';

    print '<pre>';
        print_r($_SESSION["fb_user_groups"]);
        print '</pre>';









    }
    else
    {
        //display login url 
        $login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
//not logged in goes here       
?>
<center><a href="<?php echo $login_url; ?>">Login</a>
<?php
    }
}

需要facebook sdk我删除了我脚本中的所有数据库插入,但这将有助于您了解如何执行此操作。 这将为您提供所有用户信息和阵列中的用户组,只需10/14/15