How to list your friends, and see if they are friends among themselves already

From Facebook Developer Wiki

Jump to: navigation, search

WHAT I AM SHARING BELOW

STEP 1

The aim is to be able to list your friends' names fully, in text.

STEP 2

And once we can list them fully, we can take another step, to check if each friend is already friendly with another one in your list.


DETAILS

STEP 1

The aim is to be able to list your friends' names fully, in text. (You can extend this to listing other properties on your own later.)

Here's the code first.

  <?php
  
  require_once 'facebookapi/php/facebook.php';
  
  $appapikey = 'your own api key';    //CHANGE THIS 
  $appsecret = 'your own secret key'; //CHANGE THIS 
  $facebook = new Facebook($appapikey, $appsecret);
  $user_id = $facebook->require_login();
  
  
  
  
  $fb_user = $facebook->user;
  
  $friends = $facebook->api_client->friends_get();
  $friends = array_slice($friends, 0, 10);
  
  $i=0;
  foreach ($friends as $friend) 
  {
    $personArray = $facebook->api_client->users_getInfo($friend,"name");
    $person[$i]=$personArray[0];
    $i++;
  }
  
  
  $i=0;
  foreach ($person as $f)
  {
    echo " ".$f['name'];
  
    //MORE DETAILS HERE IN STEP 2
  
    echo "<br />";
    $i++;
  }
  echo "<br />";

For the sake of practice, I only sliced the full friends list into 10 friends, so that the processing will not take too long. Already, with my slow server it already takes 1.5 minute to generate. You will have better luck with a good server and probably better bandwidth. If you get a Facebook message saying the app is not responding, tailor down the slice accordingly.

I find that it is more efficient to bring down the information into a $person array first. Hence the first loop.

The second loop is where the list of your current friends are displayed.

Note that I said //MORE DETAILS HERE IN STEP 2. There is a chunk shown below that will then check which friends know which other friend of yours.


STEP 2

Here you go. The chunk of code, for inserting there, that will show you who is the a friend of some other friend of yours.

    $j=0;
    foreach ($person as $fof)
    {
      if($i == $j) break;  // if both id's are same then friends_areFriends gives error
      $isfofarray = $facebook->api_client->friends_areFriends($person[$i]['uid'],$person[$j]['uid']);
      $isfof = $isfofarray[0]['are_friends'];
      if ($isfof==1) echo "+".$fof['name'];
      $j++;
    }

FUTURE ENHANCEMENTS

There are many possibilities that you can try. One most common one, is to display their photos as well, so that you can see everything visually.

One improvement you should do, is to eliminate some repetitions, like some couples who are shown all over again as couples because you know both people.

The best one I've seen and learned from is actually this great guy who made everything into a graphical interactive map. visualcomplexity.com

reference