O'Reilly logo

Facebook Cookbook by Jay Goldman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Get Groups

Problem

I need to retrieve information about a set of groups, based on either a list of users or group IDs.

Solution

Use the Groups.get() method:

$groups = $facebook->api_client->groups_get();

If you specify no filters, you’ll get all the groups for the current loggedinuser. You can specify a different user’s uid to filter for them:

$groups = $facebook->api_client->groups_get(12345);

In that case, you’ll get something back only if the current loggedinuser is allowed to see the groups for the user you’ve specified. If you have one or more gids (group IDs) and just want to pull information on them, you can do that too:

$targetGroups = array('2248774311', '14740918186');
$groups = $facebook->api_client->groups_get(null, $targetGroups);

Discussion

Groups.get() returns a multidimensional array of group records, with each element containing the fields listed in Groups Table.

FQL equivalent

If you’d prefer to use FQL to access groups, the equivalent query is:

SELECT gid, name, description FROM group WHERE gid IN (SELECT gid FROM
 group_member WHERE uid=$uid) AND gid IN ($gids)

See Groups Table for more information.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required