twilio catching error does not work

岁酱吖の 提交于 2019-12-06 06:25:24

The class is in a namespace, so I have to reference the absolut class exception - \Services_Twilio_RestException - in the catch .

It works with this code:

    try {
        \Twilio::message( $toNumber, 'Pink Elephants and Happy Rainbows');
    } catch ( \Services_Twilio_RestException $e ) {
        elog( 'EACT', $e->getMessage(  ) , __FUNCTION__ );  
    }

See below which is valid as of today. TwilioException is not valid and neither is Services_Twilio_RestException. You should use Exception instead.

My use case is I had to send to a database of numbers and not have an invalid phone number break my script. We did some work-around a month or two ago which involved logging when a message was sent and had a cron job checking where we left off every two minutes... not efficient when you're sending tens of thousands of text messages.

require_once '../Twilio/autoload.php'; // Loads the library

use Twilio\Rest\Client;

//some test fail numbers
$arr = array(1234567890,"11855976lend1",321619819815,198198195616516);


/* ==================================================================================
//create a function to send SMS using copilot (uses an SID instead of a phone number)
   ================================================================================*/
function sendSMS($to){
  // Download the PHP helper library from twilio.com/docs/php/install
  // These vars are your accountSid and authToken from twilio.com/user/account
  $account_sid = 'xxx';
  $auth_token = 'xxx';
  $client = new Client($account_sid, $auth_token);

  //this nifty little try/catch will save us pain when we encounter bad phone numbers
  try{
    $client->messages->create(
      $to,
      array(
          'messagingServiceSid' => "MGxxx",
          'body' => "This is the body we're sending."
      )
    );

    //sent successfully
    echo "sent to $to successfully<br>";
  }catch(Exception $e){
    echo $e->getCode() . ' : ' . $e->getMessage()."<br>";
  }

}


foreach($arr as &$value){
  sendSMS($value);
}

//remember to unset the pointer so you don't run into issues if re-using
unset($value);

Today (19-May-2017) the code is like this :

    // Step 1: set our AccountSid and AuthToken from https://twilio.com/console
    $AccountSid = "XXX";
    $AuthToken = "XXX";

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

    try {
        $sms = $client->account->messages->create(

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

            array(
               // Step 2: Change the 'From' number below to be a valid Twilio number
                // that you've purchased
                'from' => "+XXXXXXXXXXX",

                // the sms body
                'body' => $sms
            )
        );

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

    } catch (TwilioException $e) {
        die( $e->getCode() . ' : ' . $e->getMessage() );
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!