Hi,
This is the continuation of my previous post, were I explained how to create and work with Facebook session in your website. First you need to download the facebook.php class file. This is basically the back bone of Graph API. Click here to download the entire source code of this post. In this post I will be demonstrating how to get a list of all the friends in Facebook, along with their profile thumbnail, username, first/last name and profile ID.
Now Facebook provides an excellent API, known as FQL. This stands for Facebook Query Language. In FQL there are few predefined set of tables. All you have to do is to run simple SQL queries on those tables and fetch data. But since these are provided by Facebook, so we will not be using our conventional mysql_connect() etc to connect to the database. This is where the Graph API comes into play.
For running a FQL query this is what you need to write:
$fql = "YOUR_FQL_QUERY";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$result_set = $facebook->api($param);
The $facebook->api() is the Graph API. $param array is defining the task that need to be performed, in this case it is a FQL query. Before getting into the explanation of this post, lets see how the page looks:
In the above I have listed all the friends thumbnails in <ul>, <li> form. When you click on any of the thumbnails. you are taken to its profile page.
Code & Explanation:
Here is place holder the friends thumbnails to be displayed :
<ul style="padding:0px">
<?php
try{
// MY FQL
}
catch(Exception $o){
d($o);
}
foreach($friend_list as $rec){
?>
<li style="float:left;list_style:none;margin:5px">
<a href="https://www.facebook.com/profile.php?id=<?php echo $rec['uid']; ?>" target="_blank">
<img src="<?php echo $rec['pic_square']; ?>" title="<?php echo $rec['name']; ?>">
</a>
</li>
<?php
}
?>
</ul>
In the above above I am using a result set $friend_list, which is the friends array. Now I need to take care of the FQL as:
$fql = "select name,username,pic_square,uid from user where uid in (select uid1 from friend where uid2=" . $uid.")";
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
Facebook provides a "user" table with the details of the various facebook users and a "friend" table containing the ID's of all the friends in Facebook. So I simply run the following query:
"select name,username,pic_square,uid from user where uid in (select uid1 from friend where uid2=" . $uid.")";
This is a self explanatory query. What is important here is the $uid. This is what I am getting while fetching users facebook session details from:
$uid = $facebook->getUser();
$me = $facebook->api('/me');
That's all you need to do. Download: fb_friends.zip
gracias por el post, me ha servido bastante!!! Saludos desde méxico
ReplyDeletehi
ReplyDeletehow to send access token via params array
I try to use your example but it couldn't work properly.
ReplyDeleteI think "$session = $facebook->getSession();" this part does not work.
I checked '$session' then there is nothing so first if operator also not work.
And now page show 'not logged in' page and when I click 'log in' link when browser doesn't have log in info then move to log in page and then I log in this then move to my facebook's timeline.
(even if I logged in so browser already have log_in_info it also show 'not logged in' page and clcick 'log in' then it also move my facebook's timeline.)
How can I solve this problems?
Please tell me.. I wait for your answer..
unfortunately i think facebook trashed the api that this uses since facebook wants people to use the new api.
ReplyDeletewondering if there is a work around to get this code working again?
i am so sick of companies just throwing old features out and making people spend hours and hours recoding just to get the same functionality!