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:
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}";
}
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.
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!"
);
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.