Configuring Twilio Php Library In Yii 1.1.12

I am trying to configure Twilio on Yii to send SMS but not able to configure it. Unfortunately, Twilio is also not able to help much. Following is what I have done so far:

Downloaded Twilio PHP Library from this link.


Copied the folder to protected/vendors directory


Imported the twilio folder and added "require" to the main file “Twilio.Php” in the code. This I came to know after I approached Twilio but the problem still persists.Tried both, “require_once” and “require” Modified config.php file and added following:



'application.vendors.twilio-php.Services.*', 

'application.vendors.twilio-php.Services.Twilio.*', 

'application.vendors.twilio-php.Services.Twilio.Rest.*', 



Also tried unloading Yii base and then adding references as mentioned inthis post.

I even tried adding explicit references of the following but that didn’t even work:

Services/Twilio/Rest/Accounts.php


Services/Twilio/HttpStream.php


Services/Twilio/Resource.php

Following is my development environment:

Apache 2.2.22


PHP 5.3


Yii Framework 1.1.12


Windows-7 64-Bit

Following is my code:




public function SendSMS(){

             Yii::import('application.vendors.twilio-php.Services.Twilio.Resource.php');

             Yii::import('application.vendors.twilio-php.Services.Twilio.Rest.Accounts.php');

             Yii::import('application.vendors.twilio-php.Services.Twilio.ListResource.php');

             Yii::import('application.vendors.twilio-php.Services.Twilio.HttpStream.php');

             include 'C:\Projects\EMR\webapp\protected\vendors\twilio-php\Services\Twilio.php';

             $AccountSid = "SID";

             $AuthToken = "Token";

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

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

                                      "111-222-3333", // From this number

                                      "9999999999", // To this number

                                      "First PHP Test message!"

                                       );

            // Display a confirmation message on the screen

            echo "Sent message {$sms->sid}";

}



Following is the screen shot of error that I get:

The problem is with the library autoloaders; take a look at this post. You have to place that code before your controller’s class definition and delete the imports and includes from the function.

Problem solved!!

Many thanks JFReyes.

Following is the explanation of my solution.

The problem was the way I was unloading Yii. Initially, I was unloading Yii autoloader but in slight incorrect way. My initial approach was to unload Yii and then do all the Twilio tasks and then load it back. This was incorrect. The correct way is to to unload Yii, load twilio files and right after loading twilio, load the Yii back and then do the other tasks. Following is what my code looks like:




Yii::import('application.vendor.twilio-php.*');

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

require 'C:\Projects\EMR\webapp\protected\vendors\twilio-php\Services\Twilio.php';

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

$AccountSid = "AcoiuntSid";

$AuthToken = "Token";

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

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

"00000000", // From this number

"99999999", // To this number

"First PHP Test message!"

);



I’m glad you could solve the problem… :)

Did this work on your server environment? Cause when I tried the twiliosms extension it run on my local server but not online :S

Yes, it worked for me online as well as on my server.

woohoo, me too. Thanks for the instructions you left mate, it was super easy :)

Thanks, I’m glad that this post helped :)

So I have a problem. I am developing an iphone companion app for my web app, and when I try to use twilio in the same manner as in my web app it doesn’t work. The sms doesn’t get sent.

This is my code:


Yii::import('application.vendor.twilio.*');

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

					require dirname(__FILE__).DIRECTORY_SEPARATOR.'../vendor/twilio/Services/Twilio.php';

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

					


					$sid = "blah blah"; // my Account SID from www.twilio.com/user/account

					$token = "blah blah"; // my Auth Token from www.twilio.com/user/account

					 

					$client = new Services_Twilio($sid, $token);

					$message = $client->account->sms_messages->create(

					  '+number', 

					  '+other number', 

					  $_POST['message']

					);

Can anyone help me please?

EDIT

And I made the post to the same controller as my web app function, instead of my iOS api. I have code to also send an email with the same $_POST[‘message’] and that works just fine, so there’s something wrong with this block of code, or rather something wrong with my useage of it, since from the webapp it works just fine.