Getting a user's friends' info

By default, you only have access to a user's friends' Facebook id and their name. Having access to their Facebook id also gives you access to their profile photo.

You can, in theory, (I haven't tried yet) get more information about a user's friends. If you need that functionality, you should read up on extended permisions on Facebook's Developer site. I'm assuming (but haven't tried yet) that my getFriends() function in my facebook_model file will work with additional information about a user's friends. The getFriendsArray() function will not (since it builds a simple array).

To get those extended permissions working, you will also need to modify your Facebook login button. For example, <fb:login-button perms="email,user_birthday"></fb:login-button> gives you access to a user's e-mail address and birthday.

Test 2

Let's create another function in our controller to access your friends' ids and names. Add the following code to your facebooktest.php file in your controllers folder.

	function test2(){
		$data = array();
		$data['friends'] = $this->facebook_model->getFriends();

And copy and paste and save the following code as test2.php in your views/facebooktest folder.

<!DOCTYPE html>
<html xmlns=""
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title>Kent is Learning CodeIgniter - Test 2</title>
<fb:login-button autologoutlink="true" 

<div style="width:600px;">
<? if(isset($friends)){
	foreach($friends as $friend){ 
		<img src="<?=$friend['id'];?>/picture" 
			title="<?=$friend['name'];?>" />

<div id="fb-root"></div>
<script src=""></script>
  FB.init({appId: '131334533555448', status: true, cookie: true, xfbml: true});
  FB.Event.subscribe('auth.sessionChange', function(response) {
    if (response.session) {
      // A user has logged in, and a new cookie has been saved
    } else {
      // The user has logged out, and the cookie has been cleared

The important stuff is in between the <div style="width:600px"> and </div> tags. Basic stuff. It loops through all the user's friends, and displays their photo with the friend's name in the title attribute. You can see this in action by visiting our test2 view. By the way, you can also get the logged in user's profile picture at


Being able to access a user's Facebook information is exciting. And it's something I'm still just scratching the surface of. Hopefully this tutorial will help you get started in adding new functionality to an existing CodeIgniter site, or the spring board in creating a new site. If you find this useful, let me know, either in the comments below or using the contact link at the top of this page. And I'm sure other CodeIgniter users would love to see how you are connecting Facebook with CodeIgniter.

Update - July 16, 2010

I've gotten a couple of emails from people with suggestions for this tutorial, or who have had errors. You can find that information on the brand new page 5.


Kris Noble

24 June 2010 8:46 am

A helpful and detailed tutorial.

I would disagree on Model vs Library as Facebook isn't really an entity in your system but an external entity. E.g. entities such as customers, products, or orders would have models containing the domain logic that you then use to interact with the data underneath, whereas here you are just asking for and receiving data from an external source.

Looking forward to more!


15 July 2010 1:09 am

You're brilliant Kent! I just fell in love with you bro. Thanks for a great tutorial!


15 July 2010 1:47 am

really nice can we get code in zip file


25 August 2010 12:44 pm

I tried your tutorial in a fresh codeigniter installation but the user object is empty ...


13 October 2010 10:32 am

Thank you very very much for your tutorial, it's the best I found!

greetz from switzerland

Your comment will appear once it is approved.

  • (required)
  • (required - will not be published)
  • (optional)