简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM