In this tutorial I am going to show how you can access third party libraries in CodeIgniter in 4 simple steps. Now Why do we need it..?? While developing web applications, mostly developers have to deal with an integration segment i.e. they have to integrate applications with third party API’s. To segment these third party libraries with your own libraries, CodeIgniter has brought a new segmented folder i.e. third party folder.
So, if you are using CodeIgniter then you have to put that third party API libraries within the same library folder where you have kept or created your own application libraries. It is recommended to place third party packages in the application/third_party folder. In my case I’ll be using simple_html_dom.php as third party library.
Download The Script File To Use simple_html_dom Third Party Library
Extract the downloaded files, save it in your local server and run it using path :http://localhost/codeigniter_third_party/index.php/third_party_controller
Steps To Integrate third party library class in CodeIgniter:
Step 1: First download a third party library class. Here we are using simple_html_dom.php.
Step 2: Now copy it on CodeIgniter’s third party folder located at application\third_party.
Step 3: Write the code given below in your controller’s constructor to include the file in your project.
include APPPATH . 'third_party/simple_html_dom.php';
Note : If your library class exists within sub folder directory then you can write full path as:
include APPPATH . 'third_party/folder_1/folder_2/simple_html_dom.php';
Step 4: Create controller file third_party_controller.php and include the third party library in the constructor.
Controller: third_party_controller.php
In this controller first add third party folder library by including APPPATH in constructor and then in index function create object ($html ) of that class then call its function for performing specific task.
<?php
class third_party_controller extends CI_Controller {
function __construct() {
parent::__construct();
// Path to simple_html_dom
include APPPATH . 'third_party/simple_html_dom.php';
}
function index() {
// Create object of Simple_html_dom class
$html = new Simple_html_dom();
// Use Simple_html_dom class function load_file
$html->load_file('http://www.google.com');
// Use Simple_html_dom class function
foreach ($html->find('img') as $element) {
echo $element->src . '<br>';
}
}
}
?>
Conclusion:
Now you can access Third Party Libraries easily. Hope you like this tutorial. Keep reading our other blogs posts for more coding tricks. 🙂
You may also like :
CodeIgniter – Getting Started With MVC
CodeIgniter Installation and Configuration
References:
Download the simple_html_dom.php library file from here to test it at your end.
You may also like:-