require_login(); // The call back url for all internal links on this page. $appcallbackurl = 'http://www.abluestar.com/dev/facebook/'; // catch the exception that gets thrown if the cookie has an invalid session_key in it try { if (!$facebook->api_client->users_isAppAdded()) { $facebook->redirect($facebook->get_add_url()); } } catch (Exception $ex) { // this will clear cookies for your application and // redirect them to a login prompt $facebook->set_user(null, null); $facebook->redirect($appcallbackurl); die( "can not load facebook class" ); } echo "

hello $user

"; // Get a list of all your friends // This list can be returned in a serveral different formats including XML, // The friends_get converts the XML response in to an array for me and returns it. // If there was an error, friends would be set to false. // Example response /** 21005955 129500593 305200581 */ $friends = $facebook->api_client->friends_get(); if( $friends == FALSE ) { die( "could not retive this users friends list" ); } // Get the names of all my friends by there UID numbers // We generate a request for all your friends names. // We could request each users names one by one or we can save some time and some DB resources by // requesting all our friends names in one big request. $uids1 = ''; foreach( $friends as $friend ) { $uids1 .= $friend .", "; } $uids1 .= "0"; // Make the request for a users information. // You must be able to see this users information from your account. // Unless you are all ready friends you will not be able to get there name. $users_getInfo = $facebook->api_client->users_getInfo( $uids1, "name" ); // Example response // /* 732945108 Peter Dillon */ // Re orginze the data in to a useable format. $Offset = 0 ; foreach( $friends as $friend ) { $Names[$friend] = $users_getInfo[$Offset++]['name'] ; } // Query each user with every other user in your friends list and check to see if they are friends. // There will be n*n queries to the database, this will take the bulk of the proccess time. // Get the popularity between my friends foreach( $friends as $friend ) { $yourFriends[$friend] = 0 ; // Set the initial count of friends to zero $uids1 = ''; // Set the inial query line to nothing. $uids2 = ''; // Set the inial query line to nothing. foreach( $friends as $friend2 ) { if( $friend == $friend2 ) { continue; } $uids1 .= $friend . ", "; $uids2 .= $friend2 . ", "; } $uids1 .= '0'; $uids2 .= '0'; // We want to check if two my friends are friends with each other. // to do this we use the friends_areFriends function that returns a bool if they are friends. // Example response // http://wiki.developers.facebook.com/index.php/Friends.areFriends /** 510848501 504905428 0 */ $friends_areFriends = $facebook->api_client->friends_areFriends( $uids1, $uids2 ); // Re orginze the data in to a more useable format. $Offset = 0 ; foreach( $friends as $friend2 ) { if( $friend == $friend2 ) { continue; } if( $friends_areFriends[$Offset++]["are_friends"] == '1' ) { $yourFriends[$friend]++; } } } // array_combine — Creates an array by using one array for keys and another for its values // Sort the list by the number of commen friends then display the list with the friends name instead of there ID. echo "
";
arsort( $yourFriends ) ; 
$friendsIds = array_keys( $yourFriends ); 
foreach ( $friendsIds as $Id ) {
	echo "". $Names[$Id] . ": " .$yourFriends[ $Id ] . "\n"; 
}
echo "
";