8

Good mor(eve)ning guys. My question is a bit general: How can I adapt any PHP library (like facebook sdk for example) to use in CodeIgniter?

Generally, when you download a PHP library and look to the examples provided, you load the library using include or require_once. What are the adjustments (and ways) to use $this->load->library($name, $params)?

And how can I use the library after that: replacing $var = new Library($data) by ???

If my question is not yet clear, please notify me.

(bonus question: How to apply this to facebook-sdk ?)

Thanks in advance.

Hassen
  • 6,966
  • 13
  • 45
  • 65
  • you can add libraries to application/libraries in codeignter – Duke Mar 12 '12 at 10:22
  • I did that. But what about if the library contains a **require_once**? In facebook.php file, you have a **require_once("base_facebook.php")** – Hassen Mar 12 '12 at 10:30
  • There is nothing stopping you from directly including classes include(APPPATH.'libraries/Facebook/base_facebook.php'); – Duke Mar 12 '12 at 10:33

3 Answers3

9
  1. create a folder to place a facebook-sdk files in it: /application/libraries/facebook/
  2. create a Facebook_lib.php in the libraries root with the content:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    require_once 'facebook/facebook.php';
    class Facebook_lib extends Facebook{}

  3. in controller:

    $this->load->library('facebook_lib',$config); $this->facebook_lib->clearAllPersistentData();

Mikhail
  • 2,542
  • 4
  • 29
  • 40
  • 1
    I wrote a similar, slightly more detailed answer here http://stackoverflow.com/questions/11625458/integrate-phpgrid-with-codeigniter/11645624#comment15735065_11645624 – jimbo2087 Aug 23 '12 at 12:29
1

For facebook sdk you just need to copy your files into ../application/libraries/ folder and in a controller you can call it in either ways:

$config = array('appId' => APP_ID, 'secret' => APP_SECRET);
$this->load->library('facebook', $config);

or

create a file named facebook.php in ./application/config folder and create an array in it

$config = array('appId' => APP_ID, 'secret' => APP_SECRET);

and in controller simply call your library like $this->load->library('facebook');

Code Prank
  • 4,209
  • 5
  • 31
  • 47
  • So, in general, I just have to copy/paste the PHP class file in library folder and call it in a controller like my own library (mean a library that I created for codeigniter). Is there any adjustement to make inside the imported library (in addition to: if(!defined(BASEPATH... ) ? – Hassen Mar 12 '12 at 10:33
  • no you dont need to make any adjustment in either files... just load the library and call your required functions... – Code Prank Mar 12 '12 at 10:41
1

There is nothing stopping you from directly including classes include(APPPATH.'libraries/Facebook/base_facebook.php');

OR

Placing identically named versions in your application/libraries folder.

Classes should have this basic prototype (Note: We are using the name Someclass purely as an example):

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

class Someclass {

    public function some_function()
    {
    }
}

/* End of file Someclass.php */

From within any of your Controller functions you can initialize your class using the standard:

$this->load->library('someclass');

More read http://codeigniter.com/user_guide/general/creating_libraries.html

Duke
  • 35,420
  • 13
  • 53
  • 70
  • So, do I have to read the imported class and to replace all include("file.php") by include(APPPATH."libraries/file.php") ? – Hassen Mar 12 '12 at 10:38
  • @ Hassinus Yes you can ,or you can write your own libraray ,would be better – Duke Mar 12 '12 at 10:43