Search code examples
twiliotwilio-php

twilio Uncaught exception error


Twilio newbie using test account. I followed the instructions listed here for installing Twilio php: https://www.twilio.com/docs/quickstart/php/sms

Because I was getting a certificate error, my host provider suggested I changed the CURLOPT_SSL_VERIFYPEER => false (from true). But now I'm getting this error. How to fix?: Fatal error: Uncaught exception 'Services_Twilio_RestException' with message 'The requested resource /2010-04-01/Accounts//Messages.json was not found' in

require "Services/Twilio.php";

// Step 2: set our AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACbxxxxxxx";
$AuthToken = "0cfxxxxxxx";

// Step 3: instantiate a new Twilio Rest Client
//$client = new Services_Twilio($AccountSid, $AuthToken);
    $http = new Services_Twilio_TinyHttp(
        'https://api.twilio.com',
        array('curlopts' => array(
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => 2,
        ))
    );

    $client = new Services_Twilio($sid, $token, "2010-04-01", $http);

// Step 4: make an array of people we know, to send them a message. 
// Feel free to change/add your own phone number and name here.
$people = array(
    "+13121111111" => "Curious George",
//  "+14158675311" => "Virgil",
);

// Step 5: Loop over all our friends. $number is a phone number above, and 
// $name is the name next to it
foreach ($people as $number => $name) {

    $sms = $client->account->messages->sendMessage(

    // Step 6: Change the 'From' number below to be a valid Twilio number 
    // that you've purchased, or the (deprecated) Sandbox number
        "929-xxx-xxxx", 

        // the number we are sending to - Any phone number
        $number,

        // the sms body
        "Hey $name, Monkey Party at 6PM. Bring Bananas!"
    );

    // Display a confirmation message on the screen
    echo "Sent message to $name";
}

Solution

  • Twilio developer evangelist here.

    Firstly, you should never set CURLOPT_SSL_VERIFYPEER to false in production. From the curl manual:

    WARNING: disabling verification of the certificate allows bad guys to man-in-the-middle the communication without you knowing it. Disabling verification makes the communication insecure. Just having encryption on a transfer is not enough as you cannot be sure that you are communicating with the correct end-point.

    For the sake of getting you going with Twilio though your issue is in the variable names you are using.

    At the top of the file you set:

    $AccountSid = "ACbxxxxxxx";
    $AuthToken = "0cfxxxxxxx";
    

    But when you create a Twilio you use $sid and $token.

    $client = new Services_Twilio($sid, $token, "2010-04-01", $http);
    

    If you change those to $AccountSid and $AuthToken it should work as you expected.