Facebook Application Programming Step by step tutorial - part 6
Facebook users can gather in a group based on their needs. Group is a network of Facebook users based on interest in certain things.
| Method |
Description |
| groups.get |
Showing all groups based on the filter |
| groups.getMembers |
Retrieve membership of a group |
Showing Groups
To display the group, you can use the following formula:
$facebook->api_client->groups_get($uid, $gids);
- $uid is parameter to get the group to a specific user
- $gids is parameter that contains an array of ID Group. This is optional
The method will return an array with values such as gid, name, nid, description, group_type, group_subtype, recent_news, pic, pic_big, pic_small, creator, update_time, office, website, and venue.
Here is an example of using groups.get without mentioning a specific user ID.
<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134aaaaaaaaaaaaaaaaaaa';
$secret = '3c041a7cbd797aaaaaaaaaaaaaaaaaaa';
include_once './facebook-platform/php/facebook.php';
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>
<h2>Groups<h2>
<?
$uid = null;
$gids = null;
$groups = $facebook->api_client->groups_get($uid, $gids);
echo "<h3>Array Output</h3>";
echo "<pre>";
print_r($groups);
echo "</pre>";
echo "<h3>Another Output</h3>";
echo "<ol>";
foreach($groups as $group){
echo "<li>".$group['name']."</li>";
}
echo "</ol>";
?>
Example array result as follows:
[15] => Array
(
[gid] => 63623209704
[name] => Klub Baca Navila
[nid] => 0
[pic_small] => http://profile.ak.fbcdn.net/object3/1639/7/t63623209704_322.jpg
[pic_big] => http://profile.ak.fbcdn.net/object3/1639/7/n63623209704_322.jpg
[pic] => http://profile.ak.fbcdn.net/object3/1639/7/s63623209704_322.jpg
[description] => Peminat karya sastra terjemahan, sastra pesantren, sastra lokal.
[group_type] => Common Interest
[group_subtype] => Languages
[recent_news] => Navila selalu konsisten menerbitkan karya-karya sastra dari negeri-negeri Timur
[creator] => 1395458939
[update_time] => 1249973079
[office] => Pakel Mulyo UH V no 411
[website] => http://klubbacanavila.blogspot.com/
[venue] => Array
(
[street] => Pandeyan
[city] => Jogjakarta
[state] => Yogyakarta
[country] => Indonesia
[latitude] => -7.8
[longitude] => 110.367
)
[privacy] => OPEN
)
[16] => Array
(
[gid] => 70358229888
[name] => PENULIS PEMULA
[nid] => 0
[pic_small] => http://profile.ak.fbcdn.net/object3/150/30/t70358229888_169.jpg
[pic_big] => http://profile.ak.fbcdn.net/object3/150/30/n70358229888_169.jpg
[pic] => http://profile.ak.fbcdn.net/object3/150/30/s70358229888_169.jpg
[description] => PENULIS PEMULA merupakan komunitas cair, imajiner, yang dibentuk untuk mempertemukan potensi-potensi penulis dari beragam latarbelakang. Diharapkan, PENULIS PEMULA bisa menjadi sekolah bersama bagi yang ingin mendalami soal tulis-menulis. Komunitas ini tidak membatasi diri pada usia, afiliasi politik, agama, genre tulisan, pendidikan, suku, dan sebagainya. Semua berhak menulis! Di sini, mari tuangkan ketegangan kreatif kita! Saling berbagi, dan saling menimba. Di sini, Anda bisa menjadi pemburu naskah, atau naskah Anda yang diburu Salam.
[group_type] => Student Groups
[group_subtype] => Creative Arts Groups
[recent_news] =>
[creator] => 1236819023
[update_time] => 1237580951
[office] => Perum Bale Asri Blok Q-7 Gamping
[website] =>
[venue] => Array
(
[street] => Jl Wates Km 9
)
[privacy] => OPEN
)
[17] => Array
(
[gid] => 51735947828
[name] => Virtual Intelligent Services International
[nid] => 0
[pic_small] => http://profile.ak.fbcdn.net/object3/664/105/t51735947828_196.jpg
[pic_big] => http://profile.ak.fbcdn.net/object3/664/105/n51735947828_196.jpg
[pic] => http://profile.ak.fbcdn.net/object3/664/105/s51735947828_196.jpg
[description] => Information technology company based on Jakarta and Yogyakarta.
[group_type] => Business
[group_subtype] => Companies
[recent_news] =>
[creator] => 1082022764
[update_time] => 1238220998
[office] =>
[website] => http://www.visi.co.id
[venue] => Array
(
[street] =>
)
[privacy] => CLOSED
)
Figure 3.8 List of group
Showing Member Information
You can see anyone who becomes a member of a group. Formula to see is:
$facebook->api_client->groups_getMembers($gid);
Please enter your id of the group. The result, you will get an array of user ID that the member group. You will also receive membership status information.
Here's an example usage:
<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134aaaaaaaaaaaaaaaaaaa';
$secret = '3c041a7cbd797aaaaaaaaaaaaaaaaaaa';
include_once './facebook-platform/php/facebook.php';
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>
<h2>Group Members<h2>
<?
$gid = 80002146608;
$members = $facebook->api_client->groups_getMembers($gid);
echo "<h3>Array Output</h3>";
echo "<pre>";
print_r($members);
echo "</pre>";
echo "<h3>Another Output</h3>";
echo "<ol>";
foreach($members as $member){
echo "<li>".$group['name']."</li>";
}
echo "</ol>";
?>
The result like this:
Array
(
[members] => Array
(
[0] => 1564394223
[1] => 1078527089
[2] => 1108567959
[3] => 1455886398
[4] => 1184927288
[5] => 1358360770
[6] => 1075642572
[7] => 1626207382
[8] => 1226868856
[9] => 700144751
[10] => 1651427457
)
[admins] => Array
(
[0] => 1225924230
)
[not_replied] =>
)
We can modify it to be as follows:
<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134aaaaaaaaaaaaaaaaaaa';
$secret = '3c041a7cbd797aaaaaaaaaaaaaaaaaaa';
include_once './facebook-platform/php/facebook.php';
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
?>
<h1>Yummie Tester</h1>
Hello <fb:name uid='<?php echo $user; ?>' useyou='false' possessive='true' />! <br>
Your id : <?php echo $user; ?>.<br>
<h2>Group Members<h2>
<?
$uid = null;
$gid = 80002146608;
$groups = $facebook->api_client->groups_get($uid, $gid);
$members = $facebook->api_client->groups_getMembers($gid);
echo "<h3>Members of ".$groups[0]['name']."</h3>";
echo "<h4>Members</h4>";
echo "<ol>";
foreach($members['members'] as $member){
echo "<li><fb:name uid=\"$member\" useyou=\"false\"></li>";
}
echo "</ol>";
echo "<h4>Admin</h4>";
echo "<ol>";
foreach($members['admins'] as $member){
echo "<li><fb:name uid=\"$member\" useyou=\"false\"></li>";
}
echo "</ol>";
?>
Figure 3.9 List of group's member
Previous: Facebook Programming: API Events
Next: Facebook Programming: API Page