How can I integrate Twilio into the Yii Framework?

Has anyone had success in integrating the Twilio helper library into the Yii framework?

When I try utilizing the library, it creates the Services_Twilio object and I can even dump the variable to see it, but the method "create" comes up as undefined. The actually REST API call is causing me issues:


$client->account->sms->create()... [See code below] 

The error message:


Fatal error: Call to a member function create() on a non-object in ... MyController.php on line 232 

I’ve tried dumping the $client and $client->account objects and both existed. When I dump the $client->account->sms object I get NULL.

Since both the Yii framework and Twilio use spl_autoload, I’ve had to use the methods described by people in forums to unload the Yii Base, do my thing with Twilio, and then reload the Yii Base again.

Would love to do this at the index.php level rather than at the Controller level, but when trying that I get even more confusing errors.

Lastly, when I run this code outside of Yii in a standalone PHP file and the Twilio library in the same directory, I can use the method calls just fine. SMS seems to be defined and it will execute the REST API with no problems. So I figure I must not be create the Twilio object correctly or including all of the files correctly.

Any help or insight would be greatly appreciated.

I’ve got the following setup:


In the Twilio Library in:




/protected/components/Services/Twilio.php 

/protected/components/Services/Twilio/[bunch of classes]

/protected/components/Services/Twilio/Rest/[bunch of classes] 




In the Config File (main.php):




'import'=>array( 

..... 

'application.components.Services.*', 

'application.components.Services.Twilio.*', 

'application.components.Services.Twilio.Rest.*', 

) 




In the Controller:




public function actionTwilioRest() 

{ 

$this->layout = "xmlLayout"; 


// set our AccountSid and AuthToken 

$AccountSid = "XXXX"; 

$AuthToken = "XXXX"; 


// Unload the Yii Base to prevent spl_autoload conflicts 

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( 

"+1xxxxxxxxx"=>"Dr Who", 

); 


// iterate over all our friends 

foreach ($people as $number => $name) { 


// Send a new outgoinging SMS by POSTing to the SMS resource */ 

$sms = $client->account->sms->create( 

$number, 

"YYY-YYY-YYYY", 

"Hey $name, Monkey Party at 6PM. Bring Bananas!" 


); 

echo "Sent message to $name"; 

} 

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

}



References

I took a look at these topics when trying to debug the issue:

Example using CodeIgniter

  • shivaasgulati.info/blog/2010/06/twilio-rest-api-codeigniter/

Solution:

Change:


$client->account->sms->create()... [See code below] 

To:


$client->account->sms_messages->create()... [See code below] 

The tutorial code on the Twilio site used an invalid class name, hence the error. If anyone does have an idea on how to move the creation of the Twilio object out of a specific Controller Action and into a component or elsewhere, do let me know.

I’d like to be able to create the client object by doing something like this:


$client = $this->twilioapi->newClient(); 

hi, have you made this working already? thanks