Reputation: 2496
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:
I even tried adding explicit references of the following but that didn’t even work:
Following is my development environment:
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
Any help is much appreciated!!!
Upvotes: 1
Views: 825
Reputation: 13
It recommend you first unregister the Yii autoloader, then just let twilio load its classes and register the Yii autoloader again. In this scenario the twilio folder is in protected/vendors/twilio:
Yii::import('application.vendors.*',true);
spl_autoload_unregister(array('YiiBase','autoload'));
require_once(Yii::app()->basePath."/vendors/twilio/Services/Twilio.php");
spl_autoload_register(array('YiiBase', 'autoload'));
Upvotes: 0
Reputation: 1169
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 Yii::app()->params['TwillioIncludePath'];
spl_autoload_register(array('YiiBase','autoload'));
Upvotes: 3
Reputation: 2496
I got it working.
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!"
);
Upvotes: 3
Reputation: 5094
change your these statements
Yii::import('application.vendors.twilio-php.Services.Twilio.Resource.php');
to
include Yii::getPathOfAlias('application.vendors.twilio-php.Services.Twilio') . '/Resource.php';
Upvotes: 0