PHPEveryday.com PHP and Web Development Tutorial
What are you looking for?


Table of Content
Facebook Programming

First Application
Facebook Platform
First Facebook App

Facebook API
Facebook API
API Friends
API Events
API Groups
API Page
API Users

Facebook Markup Language
6 Type of FBML
FBML Users and Groups
Content Conditionally
fb:if-can-see
fb:if-can-see-photo
fb:if-is-app-user
fb:if-is-friend-with-viewer
Content in Profile Boxes
fb:if and fb:switch
UI Element and Widget
Using UI Element
Discussion Board
Creating Wall in Application
Building Request and Invitations

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





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

blog comments powered by Disqus

Database Tutorial
  • Learn PHP MySQL
  • Learn PHP ADOdb
  • Learn PHP Data Object/PDO
  • Learn PHP XML
  • Learn PHP SimpleXML
Security Tutorial
  • Learn PHP Security
  • Learn HTTP Authentication
  • Learn PHPSecureSite
Framework Tutorial
  • Learn CodeIgniter
  • Learn Joomla
  • Learn Smarty
  • Learn Zend Framework
Template Tutorial
  • Learn Joomla Template
  • Learn WordPress Template
API Tutorial
  • Learn Facebook
JS Framework Tutorial
  • Learn MooTools
  • Learn JQuery
AJAX Tutorial
  • Learn AJAX in 10 Minutes
  • Learn AJAX Client Side
  • Learn AJAX PHP
  • Learn AJAX Remote Server
  • Learn AJAX Repetitive
  • Learn AJAX MySQL
  • Learn AJAX Grid
Web Services Tutorial
  • Learn Web Services NuSOAP
  • Learn Web Services WSDL
  • Learn Web Services WSDL Array
  • Learn Web Services .NET Grid
  • Learn Web Services WDDX
Package Post
  • Joomla Intro
  • Joomla Component
  • Joomla Module
  • Joomla MVC
  • Joomla MVC Backend
  • PostNuke Intro
  • Zend Framework Intro
  • Zend Framework Action
  • Zend Framework Database
  • Zend Framework Registry
  • Zend Framework Config
  • Zend Framework Login
  • Zend Framework Session
  • PHP Array Tips
  • PHP File Tips
  • PHP Email
  • PHP Ms Excel
  • PHP Pattern
  • PHP SMS
  • Flash Database
  • PHP Multitier
  • jQuery Introduction
  • jQuery Selectors
  • Portable Web Server
  • Web Mobile Intro
  • Drupal Installation
  • Drupal Configuration