Brad Bird
Brad Bird

Reputation: 737

Codeigniter and Facebook API

I have been trying to get the Facebook SDK to work with Codeigniter but it's just not working. I've Googled the problem for about 8 hours now so I've finally given in and come here.

1) I placed the facebook.php, base_facebook.php and the .crt files inside my application/libraries folder.

2) I created a controller to handle all Facebook API methods for my future Ajax integration.

3) I loaded the library into the constructor of my new controller.

This is the error I'm getting.

An Error Was Encountered
Unable to load the requested class: Facebook

My controller class looks like this.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Fbapi extends Base_Controller {

    public function __construct()
    {
        parent::__construct();
    }

    public function index() {
        $this->load->library('facebook', $this->config->item('fb_api'));
    }

}

All off my library filenames are lowercase and the the class names are UCFIRST.

I tried making a new library called test_library that echoed a response to the browser and this works perfectly fine so I'm really stumped as to why my Facebook libraries aren't being loaded...

Thank you in advance.

Upvotes: 0

Views: 1594

Answers (1)

Matthew Daly
Matthew Daly

Reputation: 9476

The reason it isn't being loaded is because it isn't a CodeIgniter library, so you can't import it as one. Instead, to access the Facebook object, you need to import the file as you would normally in PHP.

I would use something like this:

// Include the Facebook PHP SDK
require_once("facebook/facebook.php");

// Create the Facebook object
$config = array();
$config['appId'] = $this->config->item('facebook_app_id');
$config['secret'] = $this->config->item('facebook_secret');
$config['fileUpload'] = false;
$facebook = new Facebook($config);

You can then access the SDK using the $facebook object.

You'll need to amend the path in the require_once() to point to your facebook.php file, and you'll need to place the config settings for facebook_app_id and facebook_secret in your CodeIgniter config, but I've used something similar to this before and it worked fine.

If you're doing a lot of work with the Facebook SDK, you might also want to consider spinning this out into a separate CodeIgniter library and writing methods for interfacing with Facebook - in my case I did that as there were several places where I needed to check if the user was logged into Facebook and turning it into a separate library made things easier. Or you could place it in the constructor, store it as $this->facebook instead of $facebook, and have access to it throughout the controller.

Upvotes: 2

Related Questions