Using the new Twilio Services library in a CodeIgniter Application

We are currently in the process of upgrading and testing the new Twilio Services library with OpenVBX which is built on CodeIgniter. And while we’re doing a deep integration with the library we found that it doesn’t need to be that hard to get the new Library integrated in to a CodeIgniter based application.

There are many ways that this can be done, but here we’ll illustrate a very straight forward and simple way of integrating the new library.

Setting up your Library

First, download the new Twilio Services library. The library comes with documentation and examples, so just move the “Services” folder to your Application’s libraries folder (Not the CodeIgniter libraries folder). Then, if you don’t have one already, create a new helper file (we’ll call ours ‘my-twilio-helper.php’) in your Application’s helpers folder. In that file create a new function that looks like this:

file: my-twilio-helper.php

<?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;
}

?>

Using the Service

Then, whenever you need to use the Twilio API you’d use something like this:

<?php

// Load the library, call by the name of the
// helper that you put the above function in to
$this->load->helper('my-twilio-helper');  // references library/my-twilio-helper.php

// 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
}

?>

Generating Responses

You’ll also need to create a controller that responds to twiml requests. In your controller you’d respond to the call method with something like:

<?php

/**
 * Make sure the classes we need are available
 */
$this->load->helper('my-twilio-helper'); // references library/my-twilio-helper.php

$response = new Services_Twilio_Twiml();
$response->say('Hello, you will now be connected with the caller');
echo $response;

?>

That gets you started with using the Twilio Services library with CodeIgniter. The rest is up to your business needs and imagination. And, as always, the documentation is available for help getting in to the depths of the library.

I hope this helps get you off the ground with the new library, now get coding!

2 Comments

  1. Posted April 13, 2012 at 3:34 am | Permalink

    I am calling a url like this
    site_url(‘/twiml/call’) // callback url

    but I am getting, “The specified url http://xxxxx/xx/x is invalid”

  2. Posted April 16, 2012 at 8:16 am | Permalink

    Your callback url will need a route in CodeIgniter. More information about how CodeIgniter handles and configures routes can be found here: http://codeigniter.com/user_guide/general/routing.html

Post a Comment

Your email is never shared. Required fields are marked *

*
*