Using the Facebook API with Codeigniter
The Facebook PHP SDK
Facebook has an endorsed PHP Facebook API wrapper located here at github. This library gives the php developer much more intuitive access to the facebook api methods and entities. It is well documented and complete.
Download (v3.2.0)
Where do I put it?
Place both facebook_base.php and facebook.php in your application/libraries directory
How do I use it?
You can load the facebook sdk one of two ways.
- Create a config array and pass it with the loading of the library
$fb_config = array( 'appId' => 'YOUR_APP_ID_HERE', 'secret' => 'YOUR_APP_SECRET_HERE' ); $this->load->library('facebook', $fb_config); - Create a config file
facebook.phpinapplication/config<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); $config['appId'] = 'YOUR_APP_ID_HERE'; $config['secret'] = 'YOUR_APP_SECRET_HERE';
If you are writing a facebook application and/or will be using the facebook sdk throughout your application, the second option will allow you to add it to your library autoload.
Now what?
Lets re-create the example provided with the sdk to give you a comparison to be able to translate other facebook php-sdk examples to codeigniter friendly semantics.
For comparison, view the stock example.php
Now for the CI version
Controller
class Example extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$fb_config = array(
'appId' => 'YOUR_APP_ID_HERE',
'secret' => 'YOUR_APP_SECRET_HERE'
);
$this->load->library('facebook', $fb_config);
$user = $this->facebook->getUser();
if ($user) {
try {
$data['user_profile'] = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
if ($user) {
$data['logout_url'] = $this->facebook->getLogoutUrl();
} else {
$data['login_url'] = $this->facebook->getLoginUrl();
}
$this->load->view('view',$data);
}
}
View
<html>
<head>
<title>Facebook Sweetness</title>
</head>
<body>
<h1>Facebook stuff</h1>
<?php if (@$user_profile): ?>
<pre>
<?php echo print_r($user_profile, TRUE) ?>
</pre>
<a href="<?php echo $logout_url ?>">Logout of this thing</a>
<?php else: ?>
<h2>Welcome to this facebook thing, please login below</h2>
<a href="<?php echo $login_url ?>">Login to this thing</a>
<?php endif; ?>
</body>
</html>
Using the Facebook API with Codeigniter http://t.co/D8UBKeg3 via @jondavidjohn
RT @phpizer: Using the Facebook API with Codeigniter http://t.co/vxpcoCwV
RT @phpizer: Using the Facebook API with Codeigniter http://t.co/jJTaU8o9 #facebook #codeigniter #php
Thanks… I’ve been tearing my hair away since yesterday trying to get the Facebook SDK to work with CodeIgniter. You made my day!
Thanks again
What is the use of specifying facebook app settings (appID and secret) in the config file? I see that you have respecified the two values in the controller in the form of $fb_config array.
If you specify it in the config, you don’t have to specify it inline. Like I do in the example.
i m getting these error..
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.
It’s most likely because the redirect_uri is not owned by your application…
Make sure you register your facebook application correctly. You can only redirect to your own site (domain) after a login is processed.
[...] Utilizando la API de Facebook [...]
Hey man, that repo is deprecated, just FYI in case you want to update it, new one here: https://github.com/facebook/facebook-php-sdk
I will update the post, Thanks!
Hi
Thanks for this excellent code.
Could you point me in the right direction if I wanted to get this going with the JS SDK. I have read the docs on FB but I struggle to make sense of them.
Ideally I would like a user to login via the JS SDK as it looks nicer and then I would like the PHP SDK to pick up the cookie from there on.
Many thanks
i get error
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
Sounds like the Site you are attempting to use your App key & secret from, is not the same as the domain you registered the app with.
I tried your code, but getUser allways returns zero?? The loginUrl works fine, I log in and a cookie gets created, but getUser always returns 0.
Great!
This is great tutorial, gonna try this soon, thans jon, I follow you on github
Hey, I’m having a conflict with sessions. CI uses its own session class and the Facebook SDK is using native sessions. Did you have this issue? If so how did you get around it?
If you examine the
Facebookclass you can provide your own implementations of the abstract methods exposed by extendingBase_Facebookin your own way, providing methods that use the CI sessions.Awesome, great work!
Using the Facebook API with Codeigniter .. http://t.co/TyQsarSRUu /via @jondavidjohn
Thanks for this post. i was using JS API and i was totally confused but now everything is working as charm. thanks.