Retrieve your friends latest photos
Posted by mahmud ahsan on November 17, 2009 in
FB Connect, Facebook | 5 Comments
Suppose 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>';
print_r($photos);
echo '</pre>';
Run a foreach loop for $photos and create the layout to show the photos.
Enjoy!
Related Posts
If you think this article kicked ass, subscribe to the RSS feed or follow me on Twitter! Share with your friends, or leave a comment below (or better still, do both!)Comments (5)



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
@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.
Thanks. Will do.
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
For demo, please check http://homepagefaces.com