3

我正在我的 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 错误?

4

4 回答 4

13

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

它适用于以下代码:

    try {
        \Twilio::message( $toNumber, 'Pink Elephants and Happy Rainbows');
    } catch ( \Services_Twilio_RestException $e ) {
        elog( 'EACT', $e->getMessage(  ) , __FUNCTION__ );  
    }
于 2015-08-17T23:08:07.870 回答
7

请参阅下面的内容,截至今天有效。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);
于 2017-08-25T03:34:52.410 回答
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() );
    }
于 2017-05-19T11:14:54.047 回答
1

这对我有用:

$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";
}
于 2021-07-09T14:55:25.873 回答