0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help Matt. Just for my general knowledge and learning of the Codeigniter framework, is there a cleaner way of loading the Facebook classes into Codeigniter? I didn't think Codeigniter libraries had to be laid out in a certain way and was literally just a native PHP class that can be re-used more neatly within the controllers?
@BradBird Not really. Some newer frameworks like Laravel are built around Composer, which makes it very easy to use third-party libraries cleanly, but CodeIgniter is a bit old and creaky. In my case I wrote a CodeIgniter library to interface with the Facebook object, but that may not be suitable for your situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.