This steps for CI / Codeigniter Application only . Example Code is used for Twilio API . But for other 3rd party it would be quite similar but you have to change the classes used in helper file .

Download the library folder from the 3rd party website , then place that folder in your /application/libraries folder .

Then create a helper file in your /application/helper folder , lets name it my-script_helper.php . Make sure you used that _helper keyword .

Use below mentioned script on that newly created helper file

<?php

if (!class_exists('Services_Twilio')) {
	/**
	 * The main Twilio.php file contains an autoload method for its dependent
	 * classes, we only need to include the one file manually.
	 */
	include_once(APPPATH.'libraries/Services/Twilio.php');
}

/**
 * Return a twilio services object.
 *
 * Since we don't want to create multiple connection objects we
 * will return the same object during a single page load
 *
 * @return object Services_Twilio
 */
function get_twilio_service() {
	static $twilio_service;

	if (!($twilio_service instanceof Services_Twilio)) {
		/**
		 * This assumes that you've defined your SID & TOKEN as constants
		 * Replace with a way to get your SID & TOKEN if different
		 */
		$twilio_service = new Services_Twilio(TWILIO_SID, TWILIO_TOKEN);
	}

	return $twilio_service;
}

?>

Once done . Load the  helper file in your controller by using : $this->load->helper(‘my-script’);

Now you can use the twilio function / API call in your controller .

// Request the service object $service = get_twilio_service();

// make a call

try { $service->account->calls->create( ‘4150000000’, // from this number

‘4150000000’, // to this number

site_url(‘/twiml/call’) // callback url

); }

catch (Exception $e)

{ // handle any error conditions here }

 

 

How to integrate 3rd party libraries in your Codeigniter application . Example : Twilio
Tagged on:                 

Leave a Reply

Your email address will not be published. Required fields are marked *