There is some changes has been done in the Facebook PHP SDK v.3.0.0. There is no more method named as getSession. So if you have updated to the new Fecebook PHP SDK then you might have to modify your Codeigniter Facebook connect library.
You can follow these simple steps to have a working Codeigniter Facebook library.
- Create a Config file in application/facebook.php with the following code in it.
- There is two files provided starting from V.3.0.0 of Facebook PHP SDK. Create a folder in application library and name it as facebook.
- Download the Facebook PHP SDK files and place it in the folder application/library/facebook. You can download it from https://github.com/facebook/php-sdk
- Create a new file and name it as fb_connect.php in application/library and copy the following code inside that folder.
<?php include(APPPATH.'libraries/facebook/facebook.php'); class Fb_connect extends Facebook{ //declare public variables public $user = NULL; public $user_id = FALSE; public $fb = FALSE; public $fbSession = FALSE; public $appkey = 0; //constructor method. public function __construct() { $CI = & get_instance(); $CI->config->load("facebook",TRUE); $config = $CI->config->item('facebook'); parent::__construct($config); $this->user_id = $this->getUser(); // New code $me = null; if ($this->user_id) { try { $me = $this->api('/me'); $this->user = $me; } catch (FacebookApiException $e) { error_log($e); } } } } // end class
$config['appId'] = 'Your Facebook App ID'; $config['secret'] = 'Your secret Code';
Now create a Controller and name it as user, and write the following methods in it.
function loginByFacebook(){ $this->load->library('fb_connect'); $param['redirect_uri']=site_url("user/facebook"); redirect($this->fb_connect->getLoginUrl($param)); } function facebook() { $this->load->library('fb_connect'); if (!$this->fb_connect->user_id) { //Handle not logged in, } else { $fb_uid = $this->fb_connect->user_id; $fb_usr = $this->fb_connect->user; //Hanlde user logged in, you can update your session with the available data //print_r($fb_usr) will help to see what is returned } }
That is all about the coding now when ever you need to login with facebook just redirect to user/loginByFacebook and your login will handled automatically by Facebook. But you need to update the facebook() method with the actions success and failure conditions.

It worked for me. Please try all steps or send me ur code I can help in debug.
Hi,
I am using PHPStorm as IDE and I am having problem to include the SDK facebook. Anyone can suggest any tips ?
Thanks a lot !
Naim
What kind of tips you need? Are you getting any error?
It worked. Thank you
Hi!
I’ll give this a try.
Perhaps you could provide a zip file with all the files you mention to create, so that we can just drag and drop into the application folder, think about it.
Will come back after trying.
Got it working now, is there any way to post to a user’s wall?
A new post for you. http://thephpcode.com/blog/codeigniter/how-to-post-to-users-wall-on-facebook
can you provide full source code ?
Codeigniter 2.0 with facebook sdk 3.0.1 ?
Yes. Please download it from the post How to post on user’s wall on Facebook?
Having a error showing up:
Great Help your tutorial!
Im getting the following error:
Fatal error: Call to undefined function site_url() in /home/xpto/public_html/f6/application/controllers/user.php on line 33
Any help?
Is there a View File? for the views Folder?
Also, how can I bypass the install app feature and instead show the Popup one…?
I forget to mention that, you will have to load the url helper to get the function. I have done it in the auto-load config file. I think this is the preferred method is to redirect user to facebook login screen then it will work fine in all device.
Any way if you need a popup please follow this to display the login button.
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
This will open the popup window. But please make sure facebook javascript loaded in the starting of the page.Great tutorial!
I used the files you provided above and I’m having a 404 error after redirection. here is the URL:
http://www.example.com/user/facebook?code=T9zzwlj-BbmtodaClZMKtnXe2NVbupJtp33laKTCa84.eyJpdiI6IkR6bzhiRVpndGtiSzlteUZmaHVGc2cifQ.qt6-lw7LW30dE0eWnqaSeJFYopOExhzI1PtUHRBOYzmjGlPkTKg5O21U5lvbstB3-LeKdxWdPAzKmqwdlrCtdCZE_MfS_E5GPhzdzlQo7Dv_yAYutLlUZPsNXLzcgcaE&state=abb490bf6a24cc048ddfd1f2b1de036e
Can you please give me a fix on this?
Thanks.
Hi,
Yes, I have sent you an email on this. Please check it.
Hi, I have the same problem, can you please give me a fix on this? Thanks!!
can you please provide the info in your email to me too?
Please check your email.
I did and answered you already. Thanks in advance for this quick reply
wow this is awsome… thnks very much..
I’m having some trouble that’s got me pulling out my hair:
On my local machine and on the testing machine which is on Godaddy it all works fine, but on production – which is also godaddy – the returned facebook user is always null:
object(Fb_connect)#21 (13) {
["user"]=> int(0)
["user_id"]=> int(0)
["fbLoginURL"]=> string(197) “https://www.facebook.com/dialog/oauth?client_id=xxxx&redirect_uri=http%3A%2F%2Fwww.ultimatedreamteams.com%2Fbracketchallenge%2Fusers%2Fsave_fb_user&state=xxxx”
["fbLogoutURL"]=> string(0) “” ["fb"]=> bool(false)
["fbSession"]=> bool(false)
["appkey"]=> int(0)
["appId:protected"]=> string(15) “xxxx”
["apiSecret:protected"]=> string(32) “xxxx”
["signedRequest:protected"]=> NULL
["state:protected"]=> string(32) “xxxx”
["accessToken:protected"]=> string(48) “xxxx|xxxx”
["fileUploadSupport:protected"]=> bool(false)
}
There is some config difference on this server as I had to add a ‘?’ to my .htaccess rule (index.php?/ instead of index.php/) for CI to work. I’ve tried all sorts of config variations for uri_protocol but it the FB user is always 0.
I just can’t figure out where the problem is. Any ideas? Or at least how I can trouble shoot?
The production url is: http://www.ultimatedreamteams.com/bracketchallenge/home
SOLVED:
Add this to controller functions that use FB_connect:
parse_str(substr(strrchr($_SERVER['REQUEST_URI'], “?”), 1), $_REQUEST);
Whatever it is that requires the ‘?’ added to the .htacess rule messes up $_REQUEST. I had tried the above with $_GET which didn’t work as the FB api uses $_REQUEST
great Duncan man, it solved my problem also.
thanks
I followed your tutorial and after loginbyfacebook i got blank page, vever getting into facebook method.I noticed “?” in url thinking it may have caused problems, but i dont know the workaround. Please help
Solved it? To which url it was redirected after loginby facebook?
It was redirected to http://www.dilemmo.com/test/facebook?state=90d9bc88ce1bcca8f5d7b1b66270313a&code=AQAw-n4G0iJPcSRgWL5IkReM5AEJRw188Iyv42waTGXhMNuhKZsVXzl–anRvXhup8MEImXZcHDRUd1vo4ikEYOmMQqiSrc3L6kS9R1oN9_eQ5j3scaorvBxy0RHIvi5YWI9WN50T1orfrZHpyP4uyvXuNhlBLPjMl3E9St9tCUdK2N1xKu_CcAaF1eKN2qZVSE#_
and i forgot to mention that i changed the name of the controller from User to Test because of the conflict wih another User controller that i have. I also changed
$param['redirect_uri']=site_url(“test/facebook”)
in the loginbyfacebook method.Is that all there is to change after renaming to Test? I`m otherwise good with solving problems but now i dont know where to begin. Thanks for the reply
Great insight! That’s the answer we’ve been lokiong for.
I have an error message – below
Fatal error: Uncaught exception ‘Exception’ with message ‘Facebook needs the CURL PHP extension.’ in C:xampplitehtdocshsdapplicationlibrariesfacebookbase_facebook.php:19 Stack trace: #0 C:xampplitehtdocshsdapplicationlibrariesfacebookfacebook.php(18): require_once() #1 C:xampplitehtdocshsdapplicationlibrariesfb_connect.php(2): include(‘C:xamppliteht…’) #2 C:xampplitehtdocshsdsystemcoreLoader.php(824): include_once(‘C:xamppliteht…’) #3 C:xampplitehtdocshsdsystemcoreLoader.php(100): CI_Loader->_ci_load_class(‘fb_connect’, NULL, NULL) #4 C:xampplitehtdocshsdapplicationcontrollersuser.php(55): CI_Loader->library(‘fb_connect’) #5 C:xampplitehtdocshsdapplicationcontrollersuser.php(10): User->loginByFacebook() #6 [internal function]: User->index() #7 C:xampplitehtdocshsdsystemcoreCodeIgniter.php(339): call_user_func_array(Array, Array) #8 C:xampplitehtdocshsdindex.php(201): require_once(‘C:xamppliteht…’) #9 {main} thrown in C:xampplitehtdocshsdapplicationlibrariesfacebookbase_facebook.php on line 19
Please enable curl extension in your php.ini file.
So far this script seems to be great, it does show me the information I expect to get.
But one thing I wonder, where can I add more information which I would like to get from the user?
For example:
‘scope’ => ‘publish_stream,user_birthday,user_location,user_about_me, user_photos’,
Forget my last comment, I figured it out, was simple, hehe.
But one other thing I wonder, is how do I run FQLs like this?
try{
$fql = “select name, birthday_date, sex, about_me, name, current_location, email, uid from user where uid=” . $user;
$param = array(
‘method’ => ‘fql.query’,
‘query’ => $fql,
‘callback’ => ”
);
$fqlResult = $facebook->api($param);
}
Sorry for asking this, I’m just starting to learn about mobile application.
Just want to clarify about this:
Create a Config file in application/facebook.php with the following code in it.
Source code
$config['appId'] = ‘Your Facebook App ID’;
$config['secret'] = ‘Your secret Code’;
do you mean I will create a facebook.php under the application folder? And in that php file those 2 lines of codes will be included?
e.g
<?php
$config['appId'] = 'Your Facebook App ID';
$config['secret'] = 'Your secret Code';
please reply. thanks you in advance
Hi,
My application has a problem. I can’t get the user data. Could you please help me to take a look?
controller action:
function test()
{
$data['title'] = ‘Facebook API Testing’;
$data['user_id'] = $this->fb_connect->user_id;
if($data['user_id'])
{
$data['user_profile'] = $this->fb_connect->user;
}
if($data['user_id'])
{
$data['logout_url'] = $this->fb_connect->getLogoutUrl();;
}
else
{
$data['login_url'] = $this->fb_connect->getLoginUrl();
}
$this->template->load(‘template’, ‘facebook_connect/test’, $data);
}
view file:
php-sdk
<a href="”>Logout
Login using OAuth 2.0 handled by the PHP SDK:
<a href="”>Login with Facebook
PHP Session
Your Avata
<img src="https://graph.facebook.com//picture“>
Your User Object (/me)
You are not Connected.
Thanks.
This looks great! Thanks for the tips, but when I go to user/facebook it takes me to a facebook page that says an error occured, try again later. The info in the url looks like it has the correct redirect_uri any ideas what I’m doing wrong? Thanks!
Have you changed the configuration details to match with your URL and api key?
Thanks for the tutorial!
But how can i access user’s friends data (i.e friends’ names) ?
thanks
Hi, nice tutorial. Works well for me.
But how do I get the access token when a user logs in?
its working. thank you for this tutorial.
my ci ver: 2.1.2
A PHP Error was encountered
Severity: Warning
Message: include(application/libraries/facebook/facebook.php) [function.include]: failed to open stream: No such file or directory
Filename: libraries/fb_connect.php
Line Number: 2