Facebook Programming: API Friends


Facebook Application Programming Step by step tutorial - part 4. In the previous exercise, you've tried one method in Friends: get. There are several methods available to Friends:

Method Description
friends.areFriends function to match if the two users are friends or not.
friends.get function to get a list of ID friend's
friends.getAppUsers function to determine whether the user has been authorized in a particular application
friends.getList function to display group/friend list
friends.getMutualFriends function to get a list of ID mutual friend of both the user matched.

Knowing the Friend Status

You can find out whether the A and the B is friends with friends.areFriends method. You can just enter user ID A and user ID B. Use the following formula:

$facebook->api_client->friends_areFriends($uidA, $uidB);
  • $uidA = ID user A
  • $uidB = ID user B

Friends.areFriends method will produce an array with a structure like the following example:

Array ( [0] => Array ( [uid1] => 500055128 
                       [uid2] => 522303217 
                       [are_friends] => 0 ) 
        [1] => Array ( [uid1] => 500633475 
                       [uid2] => 522750967 
                       [are_friends] => 0 ) 
        [2] => Array ( [uid1] => 503333564 
                       [uid2] => 526801479 
                       [are_friends] => 0 ) 
        [3] => Array ( [uid1] => 530387073 
                       [uid2] => 527266170 
                       [are_friends] => 0 ) 
        [4] => Array ( [uid1] => 503691892 
                       [uid2] => 503491166 
                       [are_friends] => 1 ) 
 )

Value 0 means that two users are still not friends, otherwise the value of 1 means the two have been friends. Here is an example of use (please replace the example ID with the User ID of your friends):


<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134daaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7caaaaaaaaaaaaaaaaaaaaaaaa';

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>

Check friend relationship:<br>
<?

$users1 = array(500055128,500633475,503333564,530387073,503691892);
$users2 = array(522303217,522750967,526801479,527266170,503491166);
$arrstat = array('not friend', 'friend');
?>

<ul>
<?
$check = $facebook->api_client->friends_areFriends($users1,$users2);
echo "<h4>Return from Facebook</h4>";
print_r($check);

echo "<h4>Check Result</h4>";
for($i=0;$i<count($check);$i++){
	$uid1 = $check[$i]['uid1'];
	$uid2 = $check[$i]['uid2'];
	$stat = $check[$i]['are_friends'];
	$label = $arrstat[$stat];
	echo "<li><fb:name uid='".$uid1."' useyou=\"false\" /> and <fb:name uid='".$uid2."' useyou=\"false\" /> are ".$label."</li>";		
}
?>
</ul>

If you run, will produce the following output:

facebook3.1
Figure 3.1 Matching the status of friends

Showing Friend List

You can group your friends based on your needs. Example co-workers, teammates, classmates, college friends, and so on. This is called the friend list. Friend list appeared on the front page Facebok you. Please see the left menu page.

facebook3.2
Figure 3.2 Friend list

To display the friend list, you can use the formula:

$facebook->api_client->friends_getLists();

Example usage

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134daaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7caaaaaaaaaaaaaaaaaaaaaaaa';

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>

Your friend list:<br>
<?
$fls = $facebook->api_client->friends_getLists();
?>
<?
echo "<h4>Return array from Facebook</h4>";
print_r($fls);
?>
<h4>Check Result</h4>
<ul>
<?
foreach($fls as $fl){
	echo "<li>".$fl['name']."</li>";		
}
?>
</ul>

The results as follows:

Return array from Facebook
Array ( [0] => Array ( [flid] => 70331189444 
                       [name] => php ) 
        [1] => Array ( [flid] => 67370929444 
                       [name] => artis ) 
        [2] => Array ( [flid] => 56293594444 
                       [name] => kongkow ) 
        [3] => Array ( [flid] => 56248104444 
                       [name] => sekolah ) 
        [4] => Array ( [flid] => 56242989444 
                       [name] => dokter ) 
        [5] => Array ( [flid] => 56219829444 
                       [name] => buku ) 
        [6] => Array ( [flid] => 56215709444 
                       [name] => politik ) 
)
 
Check Result
php
artis
kongkow
sekolah
dokter
buku
politik

User of Application

To find users who use your application, you can use friends.getAppUsers method. Formulation as follows:

$facebook->api_client->friends_getAppUsers();

Usage example:

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134daaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7caaaaaaaaaaaaaaaaaaaaaaaa';

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>

Your friends who are users of this app:<br>
<?
$friends = $facebook->api_client->friends_getAppUsers();
?>

<?
echo "<h4>Return array from Facebook</h4>";
print_r($friends);
?>
<h4>Check Result</h4>
<ul>
<?
foreach($friends as $friend){
	echo "<li><fb:name uid'".$friend."' useyou='false' /></li>";		
}
?>
</ul>

Showing Mutual Friends

You can find out who became a friend of the A and the B. This is called mutual friends. Formulation is:

$facebook->api_client->friends_getMutualFriends($uid1, $uid2);

Usage example:

<?php
// Get these from http://developers.facebook.com
$api_key = '2c73ae8529134daaaaaaaaaaaaaaaaaa';
$secret  = '3c041a7caaaaaaaaaaaaaaaaaaaaaaaa';

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>

Mutual friends:<br>
<?
$uid 	 = 1493017625;
$friends = $facebook->api_client->friends_getMutualFriends($uid);
?>

<?
echo "<h4>Return array from Facebook</h4>";
print_r($friends);
?>
<h4>Check Result</h4>
Your friend is <fb:name uid='<?=$uid?>' useyou='false' /><br>
Mutual friend:<br>
<ul>
<?
foreach($friends as $friend){
	echo "<li><fb:name uid='".$friend."' useyou='false' /></li>";		
}
?>
</ul>

The result you can see in Figure 3.3:

facebook3.3
Figure 3.3 Viewing a mutual friend

Previous: In the previous Facebook Programming: Facebook API
Next: Facebook Programming: API Events




Bookmark and Share Tag: facebook, api Category: PHP API Post : November 10th 2009 Read: 86,995

advertisements


blog comments powered by Disqus