Twilio捕捉错误无效。

4
我正在将twilio集成到我的laravel 5应用程序中。为了在框架中使用它,我使用aloha/laravel-twilio集成。
使用test-credentials发送有效请求工作正常。当我想要实现error-handling时遇到问题。
由于某种原因,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?

1
你解决了吗?我也遇到了同样的问题。如果你已经解决了,请在这里粘贴解决方案。 - harsh4u
我得到了以下解决方案。 - harsh4u
4个回答

13

这个类在一个命名空间中,所以我必须在 catch 中引用绝对类异常 \Services_Twilio_RestException

使用以下代码可以正常工作:

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

9
请看以下内容,截至今日仍有效。 TwilioException 是无效的,Services_Twilio_RestException 也是如此。你应该使用 Exception 代替。
更新: 你需要导入 Twilio\Exceptions\TwilioException,才能让 TwilioException 正常工作。
我的用例是,我必须发送到一个号码数据库,而不会因为无效的电话号码破坏我的脚本。一个月或两个月前,我们做了一些变通措施,当消息被发送时记录下来,并有 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);

2
这是我成功的方法:

这是我成功的方法:

$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";
}

2
今天(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() );
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接