Tuesday, August 9, 2011

Intergrate Twillio and the Yii Framework

For anyone that wants to integrate the Twillio api with the Yii Framework, here's a rough guide.

Overview:
  1. Download the Twilio php api
  2. Place the Services folder into your components folder
  3. Update your controller
Those should be all the steps you need, although I'm looking for a solution to get the code out of the controller and create a Twilio object I can use when I want throughout the application. Comments on how to do that appreciated.

Download the Twilio Php API

Grab the latest Twilio helper libraries from here:
http://www.twilio.com/docs/libraries/

Place the Service folder into your components folder

Go /protected/components and place the unpacked Services folder there. You'll find Twilio.php and the Twilio folder within the Services folder.

Update the controller

Last step involves updating your action within the controller to make use of the Twilio libraries.  

First you'll want to create a REST related action in your controller. For example, "actionTwilioRest". 

public function actionTwilioRest()

You'll then want to set your AccountSid and AuthToken, which you can find within your Twilio account. Review the tutorials and helps files at Twilio for more about this.

$AccountSid = "your account sid here";
$AuthToken = "your authtoken here";

Due to the fact that Yii has an autoloader as does the Twilio library, you'll need to unload Yii's while you load up Twilio. Here's how you do that.

spl_autoload_unregister(array('YiiBase','autoload'));
require('Services/Twilio.php');

You're now ready to create your Twilio client object like so:

$client = new Services_Twilio($AccountSid, $AuthToken);

At this point, you can perform whatever Twilio functions you need to, such as looping through a list of phone numbers, etc.

When you've finished up your Twilio related operations, you're ready to load back in the YiiBase. Do so with another spl_autoload_register call.

spl_autoload_register(array('YiiBase','autoload'));

And with that, you should have a working Twilio library right within your Yii application.

Below is the full action declaration that I used.

public function actionTwilioRest()
{
  $this->layout = "xmlLayout";

  // set our AccountSid and AuthToken
  $AccountSid = "your account sid here";
  $AuthToken = "your authtoken here";

  spl_autoload_unregister(array('YiiBase','autoload'));

  require('Services/Twilio.php');

  // instantiate a new Twilio Rest Client
  $client = new Services_Twilio($AccountSid, $AuthToken);

  // make an associative array of people we know, indexed by phone number
  $people = array(
     "+155555555"=>"Joe",
     "+1155555444"=>"Boujin",
  );

  // iterate over all our friends

  foreach ($people as $number => $name) {
    // Send a new outgoinging SMS by POSTing to the SMS resource
    try {
      $sms = $client->account->sms_messages->create(
         $number,
         "YYY-YYY-YYYY",
         "Hey $name, testing the web app. You can ignore."
         );
      echo "Sent message to $name";
    } catch (Exception $e) {
      echo 'Error: ' . $e->getMessage();
    }
  }
        
  spl_autoload_register(array('YiiBase','autoload'));

}

6 comments:

Alex said...

I followed your tutorial and keep ending up with an HTML Error 500. When I comment out the following 2 lines the error disappears:

$client = new Services_Twilio($AccountSid, $AuthToken);

AND

$sms = $client->account->sms_messages->create(....)


Any ideas?

Boujin said...

Since the error occurs when you create the Twilio object, then you've got an issue with how those files are being loaded into the program. Do you have the specific 500 error message that you're getting?

My guess would be look at your components directory and make sure that you've unpacked the Twilio packages correctly to that folder. You should have /protected/components/Services (capitalization matters) and a number of sub directories and files.

Alex said...

The error i get simply says:

HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.

Is there a way to find out what the error is more specifically?

Within /protected/components/Services I have Twilio.php and a folder named Twilio which has 12 files and another folder named Rest which contains another 27 files. The require('Services/Twilio.php'); line doesn't fail..

I downloaded the "Official PHP Twilio REST API and TwiML library" from http://www.twilio.com/docs/libraries/ (twilio-twilio-php-3.2.3-0-g3e7c2bd.zip)

Boujin said...

Yeah, you'll want to look at the error logs for Apache in that case. If you email me (boujin at gmail.com), I can help troubleshoot.

Depending on your hosting environment, etc, those error log files are going to be in different locations.

Alex said...

You were right. I downloaded Twilio from a different link and reinstalled it and it seems to work now. Looks like I was missing a file or 2 due to an incomplete file upload

Boujin said...

Awesome, glad to hear it!

Very odd that the previous file would have been incomplete in some way... but happy that things worked out.