You are here: Facebook application developers forumFacebook development boardsApplication developmentUsing the FacebookRestClient Class’ “Group” Methods, Part 1
Pages: [1]
Using the FacebookRestClient Class’ “Group” Methods, Part 1

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_get

If 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.

Input

The 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.

Output

groups_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.

venueAn 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 Groups

Let’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:

Quote from: index.php: Display all the groups to which the current user belongs:
<?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:

Quote from: Output of index.php
Array
(
   
  • => Array
        (
            [gid] => 2443007448
            [name] => Bicycle Film Festival
            [nid] => 0
            [description] => The Bicycle Film Festival celebrates the bicycle...
            [group_type] => Entertainment & Arts
            [group_subtype] => Movies
            [recent_news] => Toronto BFF    Aug. 22-26th.
            [pic] => http://profile.ak.facebook.com/object2/1011/100/s2443007448_6033.jpg
            [pic_big] => http://profile.ak.facebook.com/object2/1011/100/n2443007448_6033.jpg
            [pic_small] => http://profile.ak.facebook.com/object2/1011/100/t2443007448_6033.jpg
            [creator] => 753900102
            [update_time] => 1186578476
            [office] =>
            [website] => http://bicyclefilmfestival.com/2007_site/toronto/
            [venue] => Array
                (
                    [street] =>
                    [city] => Toronto
                    [state] => Ontario
                    [country] => Canada
                )

        )

    [1] => Array
        (
            [gid] => 2204528710
            [name] => Ban Comic Sans
            [nid] => 0
            [description] => We believe in the sanctity of typography...
            [group_type] => Common Interest
            [group_subtype] => Beliefs & Causes
            [recent_news] =>
            [pic] => http://profile.ak.facebook.com/object/1798/71/s2204528710_25989.jpg
            [pic_big] => http://profile.ak.facebook.com/object/1798/71/n2204528710_25989.jpg
            [pic_small] => http://profile.ak.facebook.com/object/1798/71/t2204528710_25989.jpg
            [creator] => 3226623
            [update_time] => 1152407805
            [office] =>
            [website] => www.bancomicsans.com
            [venue] => Array
                (
                    [street] =>
                    [city] =>
                    [state] =>
                    [country] =>
                )

        )

    [2] => Array
        (
            [gid] => 2204901902
            [name] => Chicks Dig Accordion Players
            [nid] => 0
            [description] => You know it's true!
            [group_type] => Common Interest
            [group_subtype] => Beliefs & Causes
            [recent_news] =>
            [pic] => http://profile.ak.facebook.com/object/80/112/s2204901902_24959.jpg
            [pic_big] => http://profile.ak.facebook.com/object/80/112/n2204901902_24959.jpg
            [pic_small] => http://profile.ak.facebook.com/object/80/112/t2204901902_24959.jpg
            [creator] => 1654530023
            [update_time] => 1153878053
            [office] =>
            [website] =>
            [venue] => Array
                (
                    [street] =>
                    [city] =>
                    [state] => Ontario
                    [country] => Canada
                )

        )

    [3] => Array
        (
            [gid] => 2205007948
            [name] => Facebook Developers
            [nid] => 0
            [description] =>
            [group_type] => Facebook
            [group_subtype] => Internet & Technology
            [recent_news] =>
            [pic] => http://profile.ak.facebook.com/object2/1194/26/s2205007948_5657.jpg
            [pic_big] => http://profile.ak.facebook.com/object2/1194/26/n2205007948_5657.jpg
            [pic_small] => http://profile.ak.facebook.com/object2/1194/26/t2205007948_5657.jpg
            [creator] => 206186
            [update_time] => 1182192551
            [office] =>
            [website] => http://developers.facebook.com/
            [venue] => Array
                (
                    [street] =>
                    [city] =>
                    [state] =>
                    [country] =>
                )

        )

     ...and on it goes...


Finding Out Which Groups a Given User Has Joined

Suppose 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:

Quote
print_r($facebook->api_client->groups_get(563885607));

…will return a list of my events.


Getting Information About One or More Given Groups

To 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 2262236478

Use this code:

Quote
print_r($facebook->api_client->groups_get(NULL, array(2204756589, 2232313392, 2262236478)));

You should see a result that looks like this:

Quote from: Output
Array
(
   
  • => Array
        (
            [gid] => 2204756589
            [name] => Accordion Advancement Front
            [nid] => 0
            [description] => The AAF and its members are dedicated to spreading awareness of the accordion, its music, and its artists...
            [group_type] => Common Interest
            [group_subtype] => Beliefs & Causes
            [recent_news] => Did you know? According to calorie-count.com, playing accordion burns 122 calories per hour...
            [pic] => http://profile.ak.facebook.com/object/1197/111/s2204756589_35257.jpg
            [pic_big] => http://profile.ak.facebook.com/object/1197/111/n2204756589_35257.jpg
            [pic_small] => http://profile.ak.facebook.com/object/1197/111/t2204756589_35257.jpg
            [creator] => 6854746
            [update_time] => 1180982893
            [office] =>
            [website] =>
            [venue] => Array
                (
                    [street] =>
                    [city] =>
                    [state] => IN
                    [country] => United States
                )

        )

    [1] => Array
        (
            [gid] => 2232313392
            [name] => Bears Who Drive Cars
            [nid] => 0
            [description] => This group is not open to all bears, it is only for bears who drive cars....*NO FURRIES ALLOWED*
            [group_type] => Organizations
            [group_subtype] => Philanthropic Organizations
            [recent_news] => I found two "Bear City" sketches from SNL that involve bears driving cars...
            [pic] => http://profile.ak.facebook.com/object/1163/124/s2232313392_36567.jpg
            [pic_big] => http://profile.ak.facebook.com/object/1163/124/n2232313392_36567.jpg
            [pic_small] => http://profile.ak.facebook.com/object/1163/124/t2232313392_36567.jpg
            [creator] => 0
            [update_time] => 1171692397
            [office] =>
            [website] =>
            [venue] => Array
                (
                    [street] =>
                    [city] =>
                    [state] => NH
                    [country] => United States
                )

        )

    [2] => Array
        (
            [gid] => 2262236478
            [name] => addicted to Street Meat
            [nid] => 0
            [description] => a group for all people who are in love with downtown hot dogs and sausages!
            [group_type] => Common Interest
            [group_subtype] => Food & Drink
            [recent_news] =>
            [pic] => http://profile.ak.facebook.com/object/1549/36/s2262236478_36749.jpg
            [pic_big] => http://profile.ak.facebook.com/object/1549/36/n2262236478_36749.jpg
            [pic_small] => http://profile.ak.facebook.com/object/1549/36/t2262236478_36749.jpg
            [creator] => 504094726
            [update_time] => 1175655527
            [office] =>
            [website] =>
            [venue] => Array
                (
                    [street] =>
                    [city] => worldwide
                    [state] =>
                    [country] =>
                )

        )

)


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.
Logged
  • RomG
  • Newbie
  • *
  • Posts: 1
  •  
  • View Profile WWW
Re: Using the FacebookRestClient Class’ “Group” Methods, Part 1
« Reply #1 on: November 07, 2009, 12:58:04 PM »

Hi sir!  I have been following your post and just had to register and thank you!  Your posts re: facebook has really been a great help to me.
Logged

Re: Using the FacebookRestClient Class’ “Group” Methods, Part 1
« Reply #2 on: December 06, 2009, 12:37:59 PM »

The part 1 of using the facebook rest client code is very useful and I am happy to find it. Thanks for sharing it.
  • bbleo
  • Newbie
  • *
  • Posts: 11
  •  
  • View Profile
Re: Using the FacebookRestClient Class’ “Group” Methods, Part 1
« Reply #3 on: December 27, 2009, 04:18:13 PM »

Thanks for such nice info. i like
Pages: [1]
You are here: Facebook application developers forumFacebook development boardsApplication developmentUsing the FacebookRestClient Class’ “Group” Methods, Part 1
Jump to: