Using the Facebook PHP SDK with CodeIgniter
Most of my small personal projects tend to get built with CodeIgniter, which is a simple to use, fast, lightweight PHP5 MVC framework.
For a while now Iāve had an itch to build something fun against the Facebook API so I can start learning how Open Graph works, and as a primer to building a āproperā Facebook integrated application. I also realised I hadnāt actually tried using CodeIgniter 2.x since it was released (quite some time ago). With an abundance of free time this weekend it seemed like the perfect time to get hacking!
Before I could build anything I would need to know one thing: just how do you connect a CodeIgniter app to Facebook?
The answer is surprisingly simple. You will need:
Start by extracting both downloads to their own directories. Next, copy the two
PHP files
from facebook_SDK_path/src/ to
CodeIgniter_path/application/library/. To stick to the CI conventions you
should capitalise the filename of facebook.php, so it becomes Facebook.php.
The Facebook class expects some configuration details to be passed to it on
initialisation. To make this seamless with CodeIgniterās
$this->load->library() method we need to create a custom config
file. Create the file Ā CodeIgniter_path/application/config/Facebook.php
(note the capital letter), with the following contents:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'appId' => getenv('FACEBOOK\_APP\_ID'),
'secret' => getenv('FACEBOOK\_SECRET'),
);
Youāll notice Iām using getenv() to access my Applicationās ID and
Secret. Thatās because I have them stored in my Virtual Host config, but you can just
as easily specify the values directly in this file.
Right, so far, so good. The SDK files are in place, and the config file is setup. This allows us to simply use the following to load our SDK into our controller:
$this->load->library('Facebook');
Alternatively you can add āfacebookā to your libraries array in the autoload.php file.
$autoload\['libraries'\] = array('session','facebook');
Once loaded, making API calls is as simple as this:
// See if there is a user from a cookie
$user = $this->facebook->getUser();
Sample Usage
Below I have replicated the with_js_sdk.php example file in the Facebook
SDK, as a CodeIgniter controller action/view (replacing the default CI welcome
message)
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI\_Controller {
private $data = array();
/\*\*
\* Index Page for this controller.
\*
\* Maps to the following URL
\* http://example.com/index.php/welcome
\* - or -
\* http://example.com/index.php/welcome/index
\* - or -
\* Since this controller is set as the default controller in
\* config/routes.php, it's displayed at http://example.com/
\*
\* So any other public methods not prefixed with an underscore will
\* map to /index.php/welcome/
\* @see http://codeigniter.com/user\_guide/general/urls.html
\*/
public function index()
{
$this->load->library('facebook');
$user = null;
$user\_profile = null;
// See if there is a user from a cookie
$user = $this->facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user\_profile = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
show\_error(print\_r($e, TRUE), 500);
}
}
$this->data\['facebook'\] = $this->facebook;
$this->data\['user'\] = $user;
$this->data\['user\_profile'\] = $user\_profile;
$this->load->view('welcome\_message', $this->data);
}
}
/\* End of file welcome.php \*/
/\* Location: ./application/controllers/welcome.php \*/
### View
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<body>
<?php if ($user) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print\_r($user\_profile, true)) ?>
</pre>
<?php echo anchor($facebook->getLogoutUrl(), 'Logout'); ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $facebook->getAppID() ?>',
cookie: true,
xfbml: true,
oauth: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
});
};
(function() {
var e = document.createElement('script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en\_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>
</body>
</html>
If you run this code you will get a lovely print-out of the JSON data representing your Facebook account, Ā as returned by the Graph API. I added in the logout link so you can test the normal user process of logging in.
Thereās a few things Iām going to clean up in the above. Namely Iām not too keen on
passing our instance of the Facebook() class to the view. Iād rather
create a helper file and move the stuff we need to access (AppID, logout url, etc)
into a couple of functions within that⦠but itās simple enough to do this. The point
of the above code was simply to replicate one of the official samples within
CodeIgniter.
What Next?
It strikes me that most of the uses of the SDK (i.e. to get information about the visiting user), would fit best into the Model part of the CI application. To this end Iāll probably start implementing it and see how it turns out. If itās successful Iāll post the code here sometime over the weekend.