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.


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>';
echo '</pre>';

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


mahmud ahsan

Computer programmer and hobbyist photographer from Bangladesh, lives in Malaysia. My [Business | Twitter | Linkedin | Instagram | Flickr | 500px]

You may also like


  • 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?


  • 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