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();
		$this->load->view('facebooktest/test2',$data);
	}

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


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<title>Kent is Learning CodeIgniter - Test 2</title>
</head>
<body>
	
<fb:login-button autologoutlink="true" 
			onlogin="window.location.reload(true);"></fb:login-button>

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



<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<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
	window.location.reload(true);
    } else {
      // The user has logged out, and the cookie has been cleared
    }
  });
</script>
</body>
</html>

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 http://graph.facebook.com/me/picture.

Conclusion

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.

Comments

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!

Eric

15 July 2010 1:09 am

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

umefarooq

15 July 2010 1:47 am

really nice can we get code in zip file

v1r

25 August 2010 12:44 pm

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

Roger

13 October 2010 10:32 am

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

greetz from switzerland
Roger

Your comment will appear once it is approved.

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