FaisalKhan
FaisalKhan

Reputation: 2496

Configuring Twilio PHP Library in Yii 1.1.13

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:

  1. Downloaded Twilio PHP Library from this link.
  2. Copied the folder to protected/vendors directory
  3. 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”

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

  1. Services/Twilio/Rest/Accounts.php
  2. Services/Twilio/HttpStream.php
  3. Services/Twilio/Resource.php

Following is my development environment:

  1. Apache 2.2.22
  2. PHP 5.3
  3. Yii Framework 1.1.13
  4. 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

enter image description here enter image description here

Any help is much appreciated!!!

Upvotes: 1

Views: 825

Answers (4)

celarius
celarius

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

ursuleacv
ursuleacv

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

FaisalKhan
FaisalKhan

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

Let me see
Let me see

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

Related Questions