Posted by: junal on: January 20, 2008
I was searching a Facebook application example with CodeIgniter today. There weren’t many tutorials on it so decided to give a try for FB application with CI. So let’s see what and how I did it.
I’m assuming you know how to setup a FB application. If you don’t then please visit here, this is a great step-by-step process to start a FB application.
Well, if you are done setting up the process according to the instruction, you supposed to have few things in your hand: an API key, Secret key, Callback URL and base URL.
Now you need to Download the latest version of CodeIgniter and unzip the folder in your Base URL location. Set base URL from application/config/config.php
Example : $config['base_url'] = “http://apps.facebook.com/your-app-name/”;
Ok, now set your default controller from here: application/config/routes.php
Default controller is “welcome” – for sample application I didn’t change it. I just worked on default controller. You can change it according to your needs. If you want to use different controller keeping default controller as it is then add that controller name to your Callback URL.
Now let’s download the Facebook client library from here:
Once you have this package in your hand place them in your application/plugins (if there is not folder named plugins then create it) folder. Note that, you can also add the library in the library folder.There are only three files of FB client library- place them in your plugins folder. But don’t forget to rename your facebook.php to facebook_pi.php
Yay ! We are done all initial setting stuffs. Now need to see some actions!
Open you default controller (in my case “Welcome” controller).
First of all we need to create a __Contrust class so that it gets initiated for the first time application user.
So our construct class will be look like this:
var $facebook;
//API and secret key that you got from your application setup.
var $__fbApiKey = ‘YOUR-API-KEY';
var $__fbSecret = ‘YOUR-SECRET-KEY';
function __construct()
{
parent::__construct();
//load the client library that we added as plugin in plugins folder.
$this->plugin('facebook');
// Prevent the 'Undefined index: facebook_config' notice from being thrown.
$GLOBALS['facebook_config']['debug'] = NULL;
// Create a Facebook client API object.
$this->facebook = new Facebook($this->__fbApiKey, $this->__fbSecret);
$user = $this->facebook->require_login();
}
So we have called Facebook API key and secret key and then we loaded the client library that we added as plugin in plugins folder. At the end we checked if the user is logged in or not. Well, you can avoid it by defining a Session key. But I found it was easier to avoid Session key problem with the application by allowing users logged in first.
We supposed to get connected with FB API by this time. So let’s play with FB API now. Create a function called index () and lets retrieve logged in user’s all friends from it.
So it should look like this
function index()
{
// Retrieve the user's friends and pass them to the view.
$friends = $this->facebook->api_client->friends_get();
$this->load->view('welcome_message' , array("friends" , $friends));
}
So we got our friends list in “friends” array. Let’s go to “welcome_message” view in view folder to retrieve and show all friends that the array is containing. My view looks like this bellow.
<h3>Welcome to Sample Facebook application with CodeIgniter!</h3>
<div class="container">
< ?php
foreach ($friends as $id)
{
?>
<div class='img'>
<fb :profile-pic uid='<?=$id?>' firstnameonly = 'true' linked='true' size='square'/>
<div class='name'>
<fb :name uid='<?=$id?>' firstnameonly = 'true' capitalize='true' useyou = 'false'/>
</fb></div>
</fb></div>
< ?php
}
?></div>
Two facebook tags are added here. One is for Profile picture of my friends and second tag is to show my friends name. I have also added three more CSS class to show it a little bit better.

They are:
.img {
float: left;
padding : 10px 0px 0px 10px;
width:50px;
overflow: visible;
}
.name {
padding-top: 0px;
text-align: center;
}
.container
{
padding-left:18px;
}
You can add this at the top of your view page in the <style> tag.
Now let’s go to the FB canvas and see what is my page is showing. From the above example my canvas page shows the output like this:
Okay, if you want to add database, you just need to configure your application/config/databse.php file. Rest of the things are same.
Ahh ! It’s cool. I guess if I use any FW for FB application first I will check the volume of the application. I think there is no meaning to use FW for a small FB application.
[...] bookmarking this sample Facebook application with CodeIgniter mainly for the CodeIgniter aspect since i’m not really an active Facebook user. Still [...]
[...] A sample Facebook application with CodeIgniter. (tags: codeigniter facebook development fbapp application php) [...]
[...] custom plugins, I figured out how to integrate the two sets. I got most of my help from this blog post, so thanks to [...]
[...] A sample Facebook application with CodeIgniter. « Junal on the run. Posted in General | [...]
Great tut, really useful, thanks.
What’s up with the quotes? I had to convert them from curly to normal..
Hi,
nice collection of technology …..
It would be great if you upload the whole code for the classes you mention. There are some inconsistencies in your post -probably you assumed that everybody is a PHP Expert
I just added a post in my blog with detailed instruction set of how to include Facebook PHP client library into CodeIgniter 1.6.3 -clean and easy without hacking CodeIgniter
Thanks to Junal Rahman for all his support!
hello
junal i want learn php from you. tumi ki amake shikhaba.
avro
Hey!
Can i have a copy of the codes including the facebook client library, Thanks!
I used all the code carefully but at last it is giving “Page Not Found Error”. I used the default Controller(welcome).
Please send me the full code on my email.
Waiting for your Reply
Regards
Aman
Hi….Can i have a copy of the codes including the facebook client library aswell, Thanks!
i have followed ur instruction but got following error when i access my canvas page:
“The URI you submitted has disallowed characters.”
i hav set call back URL as:
http://127.0.0.1/test (this is the test local server where i have put all my CI code)..
thanx,
regards,
Shayan.
if ur having problems running this code: as in having errors like this :unknown tag ‘fb’, remove the space before the “:” to change it form this ===<fb :profile-pic== to this ===<fb:profile-pic===.
that should fix the problem. took me like 2hrs to figure!
nice article it helped me get up with my first facebook app, and I think I will wrap it in about three hours
This line was failing with unknown plugin method.
$this->plugin(’facebook’);
Per this page, I updated the code to the line below and everything worked. Thanks for this post!
codeigniter.com/user_guide/general/plugins.html
$this->load->plugin(’facebook’);
Great idea! Thank you.
Two points are needed to modify in welcome.php file:
1/ As John mentioned:
Change:
$this->plugin(’facebook’);
To:
$this->load->plugin(’facebook’);
2/ Array bug:
Change:
$this->load->view(’welcome_message’ , array(”friends” , $friends));
To:
$this->load->view(’welcome_message’ , array(”friends”=>$friends));
Good luck!
I’m running into problems setting up the welcome controller
Would it be possible for you to email exactly the code for that whole file?
that would be great,thanks
Hye everybody,
when executing $this->load->plugin(’facebook’); a weird error is displayed :
Errors while loading page from application
Parse errors:
FBML Error (line 28): illegal tag “body” under “fb:canvas”
i placed the facebook client library in application/plugins.
help pliz
thank you
ReHye
i solved the problem by adding _pi in the file name facebook.php as described above.
May google be with you.
It’s endless redirection. Redirect between my site and facebook. Here is my url: http://shopno-dinga.com/bdtourism/
any idea?
i am a foolish one the url should be http://apps.facebook.com/bdtourism/
ha ha ha. anyway, it’s solved.
[...] Examples: http://junal.wordpress.com/2008/01/20/a-sample-facebook-application-with-codeigniter/ [...]
[...] Para aquellos que quieran realizar aplicaciones en Facebook usando CodeIgniter le puede venir bien estas dos guías y dos librerías. Tanto las guías hablan de lo mismo, y las librerías son para Facebook-Connect (pero en una de ellas la explican paso a paso). A sample Facebook application with CodeIgniter. [...]
[...] A sample Facebook application with CodeIgniter. [...]
hi
i am facing this error
A PHP Error was encountered
Severity: Notice
Message: Undefined index: REQUEST_URI
Filename: plugins/facebook_pi.php
Line Number: 298
Nice example simple yet informative.
Bugs can be resolved by reading the CI documentation, on how to include plugins and correct syntax when calling the “view” part.
nice one… thanks!
[...] Junal on the Run – A sample Facebook application with CodeIgniter. [...]
[...] A sample Facebook application with CodeIgniter. [...]
January 26, 2008 at 5:01 am
I was looking to try your code. And I’m running into problems setting up the welcome controller. Would it be possible for you to email exactly the code for that whole file? That would be great. Thank you. This is a great start.