twilio捕获错误不起作用

jer*_*rik 2 php try-catch twilio laravel-5 twilio-php

我正在我的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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

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

如何获取和处理twilio错误?

jer*_*rik 13

该类位于命名空间中,因此我必须\Services_Twilio_RestException在catch中引用absolut类异常 - .

它适用于以下代码:

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


小智 7

见下文,从今天起有效。TwilioException无效,也无效Services_Twilio_RestException。你应该Exception改用。

UPDATE 您需要导入Twilio\Exceptions\TwilioExceptionTwilioException工作。

我的用例是我必须发送到数字数据库并且没有无效的电话号码破坏我的脚本。我们在一两个月前做了一些工作,其中包括记录发送消息的时间,并有一个 cron 作业检查我们每两分钟停止的地方......当你发送数万条短信时效率不高。

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);
Run Code Online (Sandbox Code Playgroud)