Get your friends details out of Facebook

The below code outputs a HTML table showing all of your facebook friends and their details.  You will need to include the code on a PHP server where you have already registered your application.

This code is far from finished, but provides a very good starting point because it shows how you can access each of the variables.


<?php
require_once 'facebook.php';
$friends = $facebook->api_client->friends_get();
echo '<table border=1>';
foreach($friends as $friend)
{
    echo '<tr>';
    echo '<td>';
    $name = $facebook->api_client->users_getInfo($friend,'name, pic_small, birthday, sex, hometown_location, meeting_sex, meeting_for, affiliations, relationship_status, significant_other_id, political, current_location, activities, interests, music, tv, about_me, work_history, education_history, status');
    echo "<img src='".str_replace('/http', '', str_replace('.jpg/','', $name[0][pic_small]))."'/>";
    echo ' </td><td>';
    echo $name[0][name];
    echo ' </td><td>';
    echo $name[0][birthday];
    echo ' </td><td>';
    echo $name[0][sex];
    echo '</td><td>';

    echo $name[0][hometown_location][city].', ';
    echo $name[0][hometown_location][state].', ';
    echo $name[0][hometown_location][country].', ';

    echo '</td><td>';
    echo $name[0][meeting_sex];
    echo '</td><td>';
    echo $name[0][meeting_for];
    echo ' </td><td>';
    echo ' </td><td>';
//    echo var_dump($name[0][affiliations]);

    foreach ($name[0][affiliations] as $aff)
    {
    echo '<ul>';
        foreach ($aff as $aff_det)
            echo '<li>'.$aff_det.'</li>';
    echo '</ul>';
    }
    echo ' </td><td>';
    echo $name[0][relationship_status];
    echo '</td><td>';
    echo $name[0][significant_other_id];
    echo ' </td><td>';

    echo $name[0][political];
    echo '</td><td>';
    echo '<ul>';
    foreach ($name[0][current_location] as $aff)
            echo '<li>'.$aff.'</li>';
    echo '</ul>';

    //echo $name[0][current_location];
    echo '</td><td>';
    echo $name[0][activities];
    echo ' </td><td>';

    echo $name[0][interests];
    echo ' </td><td>';
    echo $name[0][music];
    echo '</td><td>';
    echo $name[0][tv];
    echo ' </td><td>';
    echo $name[0][about_me];
    echo ' </td><td>';

    echo $name[0][work_history];
    echo ' </td><td>';
        foreach ($name[0][education_history] as $aff)
    {
    echo '<ul>';
        foreach ($aff as $aff_det)
        {
            echo '<li>';
            echo arrayToXML($aff_det);
            echo '</li>';
        }
    echo '</ul>';
    }


    //echo $name[0][education_history];



    echo '</td><td>';
    if ($name[0][status][time]!=0)
        echo $name[0][status][message].' - '.date("Y/m/d", $name[0][status][time]).'</td></tr>';
}
echo '</table>';
?>
Advertisements

17 thoughts on “Get your friends details out of Facebook

  1. Thanks for posting the example – its tough to find good working code snippets right now. I’ve spent most of the day struggling with getting things to work under php4. I had not idea it would be this difficult.

    To be honest, I’m still struggling some with the model that facebook uses and also with formating output to match the css that facebook uses.

    Anyway, sorry for rambling and thanks for the code.

  2. Have you had any luck with using Google maps in facebook. I’m getting stuck due to body tags cannot be used. The onload attribute of the body tag is used to load the map. Know of a good workaround?

    Thanks,
    Bailey

  3. Hi Bailey,

    ive not tried maps in facebook yet, ive actually been setting up Amazon ec2 servers all over the place the last few days.

    I have not even had time to look and the new javascript stuff they released a month or so back!

    have u tried with iframes? its an option when you create the app or you could just include your own (probably some fbml) but I expect that may also be stripped. its worth looking around to see what versions of the maps api u can find, i think its available in a number of different formats.

    If that doesn’t work it worth looking/asking on the developer forum, ive seen some pretty quick responses. and im sure its been asked before.

  4. I started going down the screen scraping route because of the restrictions in the facebook api so its been a while since I have played with this code.

    I think it may be possible, have you tried :

    foreach ($name[0][affiliations] as $aff)

    {

    echo ”;

    foreach ($aff as $aff_det)

    echo ”.$aff_det.”;

    echo ”;

    }

    echo ‘ ‘;

    echo $name[0][relationship_status];

    just a guess, and remember, var_dump() is always your friend.

  5. Hi,

    Thanks for giving a code of Get your friends details out of Facebook. When i run this code in my browser so it will give some error.

    “Warning: Invalid argument supplied for foreach() in C:\Program Files\xampp\htdocs\facebook\friend\friend.php on line 69

    so please help me for solve this error and how to display my friend list from facebook with this PHP code.

    Thanx

  6. Hi Abc,

    I have not looked at the code recently (or facebook infact) so dont have any easy answers.

    Its sounds like the array may be empty.

    You could try check to see if the var isset()

    or alternatively if you dont need that bit of data try removing the foreach statement entirely.

  7. Hi simonmcmanus

    Thankyou for giving a reply. “Get friend list” problem is solved now i want to send some messages to my selected friend using PHP so tell me how can send message to the friend?

    Please reply as early as possible.

    Thanx

  8. Hi Simon,

    i’m getting this error

    Fatal error: Call to a member function friends_get() on a non-object

    when i try to run this,
    please help me.

  9. I am experiencing trouble extracting the work locations from some users. Why is the following php code not returning
    my desired results?

    $history = $facebook->api_client->users_getInfo($uid,’work_history’);
    if ($history[0][‘work_history’][0]>1)
    {
    $work = $history[0][‘work_history’];
    print($work[‘company_name’]); // returns desired result
    print($work[‘location’][‘city’]); // returns empty
    }

  10. work_history, hs_info (high scool info), activities, education_history, current_location, affiliations, about_me and others are just blank

    1. I’ve had the same problem, but got my work_history finally working.

      First, you have to make sure you’ve got the facebook user’s permission for “work_history”.

      Second, work_history is an array of elements like ‘company_name’ and others, but facebook allows users to add multiple ‘jobs’. Each ‘job’ of the user can contain these elements.

      So if you want to get the ‘company_name’ from the user, you’ll have to get it from each job at a time

      Like this:

      $history = $facebook->api_client->users_getInfo($uid,’work_history’);

      echo ‘my first job’s company: ‘.$history[0][‘work_history’][0][‘company_name’];

      This works for me. I haven’t got “about_me” or “interests” working yet. But it could be a problem of the same nature.

  11. Nice Work…Thanks and Sir how to read USERs WALL any snippet you have.Please explain me am net to the API stuff.

Comments are closed.