Twilio捕获错误不起作用


twilio catching error does not work

我正在我的laravel 5应用程序中实现twilio。为了在框架中使用它,我使用aloha/laravel-twilio积分。

发送带有测试凭据的有效请求可以正常工作。当我想实现错误处理时,我遇到了问题。

由于某种原因,catch没有得到错误,这导致应用程序崩溃。如果我正确阅读错误消息,错误似乎在twilio-sdk中。

到目前为止我所做的是:

<?php namespace App;
use Illuminate'Database'Eloquent'Model;
use Aloha'Twilio'TwilioInterface;
class Activation extends Model {
    protected $fillable = array( 'a', 'b', 'c');
    public static function send() {
        // Testaccount
        // $toNumber = '+15005550006'; // valid number; works fine
        $toNumber = '+15005550001'; // @todo will throw an exeption, and breaks the app
        try {
            'Twilio::message( $toNumber, 'Pink Elephants and Happy Rainbows');
        } catch ( Services_Twilio_RestException $e ) {
            elog( 'EACT', $e->getMessage(  ) , __FUNCTION__ );  // this is not called when an twilio error occurs
        }
    }
}

这会导致以下错误:

Whoops, looks like something went wrong.
Services_Twilio_RestException in /path/to/my/laravel/vendor/twilio/sdk/Services/Twilio.php line 297 
Exception_message: The 'To' number +15005550001 is not a valid phone number.

从文档中应该抛出这个错误(无效的电话号码),但是我应该有可能捕获并处理它。目前,这不起作用。我没有得到错误捕获…

如何捕获和处理twilio-errors ?

类位于名称空间中,因此我必须在catch中引用绝对类异常- 'Services_Twilio_RestException

使用下面的代码:

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

如下所示。TwilioException无效,Services_Twilio_RestException也无效。你应该用Exception代替。

您需要导入Twilio'Exceptions'TwilioException才能使TwilioException工作。

我的用例是我必须发送到一个数据库的数字,而不是有一个无效的电话号码打破我的脚本。我们在一两个月前做了一些工作,包括在消息发送时进行日志记录,并每两分钟检查一次我们离开的地方……当你发送数以万计的短信时,效率不高。

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);

今天(2017年5月19日)代码如下:

    // 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() );
    }

我就是这么做的:

$twilioCli = new 'Twilio'Rest'Client(config('app.twilioAccountSID'), config('app.twilioAuthToken'));
try {
    $twilioCli->messages->create(
        $formattedToNum,
        [
            'from' => config('app.twilioFromNumber'),
            'body' => "sms body goes here"
        ]
    );
}
catch ('Twilio'Exceptions'RestException $e) {
    echo "Error sending SMS: ".$e->getCode() . ' : ' . $e->getMessage()."'n";
}