Retrieve your friends latest photos


fb_logoSuppose you want to develop a facebook application or facebook connect base application, where you want to show all the latest photos published by your friends in facebook. In this post I’m showing the solution.

blog_photos

I am just showing how do you retrieve latest photos published by your friends, I used a FQL query that retrieves latest photos of your friends.

So the solution is very simple, first retrieve the friends uid and then run a fql.

include_once "facebook.php";

$facebook       =   new Facebook($config['fb']['api_key'], $config['fb']['secret_key']);
$user             =   $facebook->user;

//this is a helper function that is used to get friend ids
function getFriendList($facebook, $appAdd=false) {
    $sql     = "";

    if ($appAdd) {
        $sql = "SELECT uid FROM user WHERE is_app_user = 1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = {$facebook->user})";
    }
    else {
        $sql = "SELECT uid FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = {$facebook->user})";
    }
    $result = $facebook->api_client->fql_query($sql);

    $exfriends      =   "";
    $flag           =   false;

    if (!empty($result)) {
        foreach($result as $item) {
            if ($flag) $exfriends .= ',';
            $exfriends .= $item['uid'];
            $flag     = true;
        }
    }
    $excludes   =   $exfriends;

    return $excludes;
}

//retrieve user's facebook friends
$friends        =   getFriendList($facebook);

//this is the fql query that solve our problem
$query1         =   "SELECT pid, aid, owner, link, src_big, src_small, created, modified FROM photo WHERE aid IN (SELECT aid FROM album WHERE owner in ($friends) ORDER BY modified_major desc) ORDER BY modified desc LIMIT 1,100";
$photos         =   $facebook->api_client->fql_query($query1);

//$photos contains all the latest photos of your friends
echo '<pre>';
print_r($photos);
echo '</pre>';

Run a foreach loop for $photos and create the layout to show the photos.

Enjoy!

mahmud ahsan

Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

You may also like

5 Comments

  • George Carter
    November 18, 2009 at 2:05 am

    Hi Mahmud,

    Good article. I’m new to Facebook and its api. The first line is:

    include_once “facebook.php”;

    What is facebook.php?

    Thanks,
    gc

    • mahmud ahsan
      mahmud ahsan
      November 18, 2009 at 2:09 am

      @George, please visit http://developer.facebook.com/get_started.php to learn more about facebook application architecture and development. If you develop facebook app using php library, then you have to include “facebook.php” from that library.

  • George Carter
    November 18, 2009 at 2:49 am

    Thanks. Will do.

  • Techchron
    December 26, 2009 at 1:43 pm

    Good article but not working in any case. Try it to have only your default “Profile Pictures” Album and add some photos into that album. You are not able to see the new ones – why?

    CThank you, Ciao
    Ralf

Comments are closed here.

About Me

Hi I am Mahmud Ahsan. I Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

Twitter Feed

Fanpage