【问题标题】:twilio catching error does not worktwilio 捕捉错误不起作用
【发布时间】:2015-08-16 10:53:43
【问题描述】:

我正在我的 laravel 5 应用程序中实现 twilio。为了在框架中使用它,我使用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 错误?

【问题讨论】:

  • 你解决了吗?我面临同样的问题。如果完成,请在此处粘贴解决方案。
  • 我得到了以下解决方案。

标签: php laravel-5 try-catch twilio twilio-php


【解决方案1】:

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

它适用于以下代码:

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

【讨论】:

    【解决方案2】:

    请参阅下面哪个是今天有效的。 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);
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        这对我有用:

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

        【讨论】:

          猜你喜欢
          • 2011-02-12
          • 1970-01-01
          • 1970-01-01
          • 2018-03-25
          • 1970-01-01
          • 1970-01-01
          • 2016-02-04
          • 1970-01-01
          相关资源
          最近更新 更多