Welcome to the fifth installment in this series on developing applications for Facebook. Currently, I’m looking at the methods in the
FacebookRestClient class.
n this article, I’ll take a look at the first of
FacebookRestClient’s “group” methods, which are:
* groups_get
* groups_getMembers
If you’re getting a sense of
deja vu it’s probably because these methods are analogous to the methods for events,
events_get and
events_getMembers.
groups_getIf you want to either:
* Know what groups a user belongs to, or
* Get information about a group that a user belongs to
then
groups_get is the method you want.
InputThe groups_get method returns an array containing a given user’s groups. It can be used to fetch all the groups for a specific user, or you can use the parameters to filter groups by group ID.
The method’s signature is:
groups_get($uid, $gids)The method parameters are explained below:
| Parameter | What It Is |
| $uid | [integer][optional] The user ID of the Facebook user whose events are to be retrieved. If this is NULL, the current user’s ID will be used. |
| $gids | [array of integers][optional] The IDs of the groups to be retrieved. If this is NULL, all the groups for the specified user will be retrieved. |
Outputgroups_get returns an array, each element of which is a group array — an associative array corresponding to a group. The elements of the group array are shown below.
| Event Array Element | What It Is |
| gid | [integer] The unique ID for the group.
|
| name | [string] The name of the group.
|
| nid | [integer] I haven't been able to find out what this is - the Facebook documentation isn't complete, and Googling hasn't turned up anything. If you know, let me know!
|
| description | [string] The description of the group.
|
| group_type | [string] The general type of the group.
|
| group_subtype | [string] The subtype of the group.
|
| recent_news | [string] The most recent news item posted to the group.
|
| pic | [string] The URL of the picture associated with the group.
|
| pic_big | [string] The URL of the large version of the picture associated with the group.
|
| pic_small | [string] The URL of the small version of the picture associated with the group.
|
| creator | [integer] The user ID of the user who created the group.
|
| update_time | [integer] I haven't been able to find out what this is - the Facebook documentation isn't complete, and Googling hasn't turned up anything. If you know, let me know!
|
| office | [string] The address of the group's headquarters.
|
| website | [string] The URL of the group's website.
|
| venue | An array containing the address details of the group.
| Array Key | What It Is | | street | [string] The street address of the group. | | city | [string] The city in which the group is located. | | state | [string] The state in which the group is located. I've looked at the groups for a number of people, and all of the states seem to be stored as their two-letter abbreviations. | | country | [string] The country in which the group is located. |
|
Getting the Current User’s GroupsLet’s take
groups_get for a spin. I’m going to assume that you’ve got a test app harness like the one we set up in the last article. The directory on your own server should have the following files:
*
appinclude.php: A short file that does some app initialization.
*
facebook.php: One of the two client library files.
*
facebookapi_php5_restlib.php: The other of the two client library files. This one contains the interesting methods.
*
index.php: The file holding the code for our test apps. When noodling around with Facebook and exploring its functions, I simply change the contents of this file and then visit the app either on my server or on its Facebook page to run it.
The following code will display all the events for the current user:
<?php
require_once 'appinclude.php';
echo("<pre>");
print_r($facebook->api_client->groups_get());
echo("</pre>");
?>
With no arguments,
events_get will retrieve the events of the currently logged-in user. Since I am the logged-in user, my events will be retrieved.
I visited
index.php on my server, which yielded a page full of PHP array representations of my events:
Finding Out Which Groups a Given User Has JoinedSuppose you don’t want the current user’s groups, but someone else’s. Getting them is easy:
* Set the $uid parameter to the user ID of the person whose groups you want to retrieve.
* Either ignore the $gids parameter or set it to NULL.
For example, to retrieve my groups, use my user ID,
563885607. A small change to
index.php as shown below:
print_r($facebook->api_client->groups_get(563885607));
…will return a list of my events.
Getting Information About One or More Given GroupsTo get the details about one or more given groups:
* Set the $uid parameter to NULL.
* Set the $gids parameter to an array containing the group IDs of the groups whose details you want.
For example, let’s suppose you want to get the details about these groups:
* The Accordion Advancement Front, whose group ID is
2204756589 * Bears Who Drive Cars, whose group ID is
2232313392 * Addicted to Street Meat, whose group ID is
2262236478Use this code:
print_r($facebook->api_client->groups_get(NULL, array(2204756589, 2232313392, 2262236478)));
You should see a result that looks like this:
Next Time…I’ll take at look at the second of the group-related methods, groups_getMembers.
This material is a property of Joey deVilla. Please,
visit his great blog for more Facebook stuff.