PHP-Twilio : Services_Twilio_RestException in Twilio.php lin


PHP-Twilio : Services_Twilio_RestException in Twilio.php line 297

我正在使用twilio PHP API来创建日期https://www.twilio.com/docs/api/rest/account instance-get-example-1

require_once('/path/to/twilio-php/Services/Twilio.php'); // Loads the library
// Your Account Sid and Auth Token from twilio.com/user/account
$sid = "ACa89d3917a5b56ebccd*********"; 
$token = "321839821309*************"; 
$client = new Services_Twilio($sid, $token);
// Get an object from its sid. If you do not have a sid,
// check out the list resource examples on this page
$account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
echo $account->date_created;

当我输入错误的帐户sid &验证令牌在twilio.php文件中显示一个错误信息,如何在我的页面上得到这个错误。

错误消息

: -

  Services_Twilio_RestException in Twilio.php line 297:
    The requested resource /2010-04-01/Accounts/AC484ce61d58339160e2052fdffe526.json was not found
in Twilio.php line 297
at Base_Services_Twilio->_processResponse(array('404', array('Access-Control-Allow-Credentials' => 'true', 'Access-Control-Allow-Headers' => 'Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since', 'Access-Control-Allow-Methods' => 'GET, POST, DELETE, OPTIONS', 'Access-Control-Allow-Origin' => '*', 'Access-Control-Expose-Headers' => 'ETag', 'Content-Type' => 'application/json; charset=utf-8', 'Date' => 'Tue, 29 Sep 2015 09:41:47 GMT', 'Twilio-Request-Duration' => '0.005', 'Twilio-Request-Id' => 'RQ488508f84e5649d1912fcccf6379b47b', 'X-Powered-By' => 'AT-5000', 'X-Shenanigans' => 'none', 'Content-Length' => '196', 'Connection' => 'keep-alive'), '{"code": 20404, "message": "The requested resource /2010-04-01/Accounts/AC484ce61d58339160e2052fdffe526.json was not found", "more_info": "https://www.twilio.com/docs/errors/20404", "status": 404}')) in Twilio.php line 265
at Base_Services_Twilio->_makeIdempotentRequest(array(object(Services_Twilio_TinyHttp), 'get'), '/2010-04-01/Accounts/AC484ce61d58339160e2052fdffe526.json', '1') in Twilio.php line 236
at Base_Services_Twilio->retrieveData('/2010-04-01/Accounts/AC484ce61d58339160e2052fdffe526') in InstanceResource.php line 79
at Services_Twilio_InstanceResource->__get('date_created') in TestController.php line 28
at TestController->index()

来自Twilio的Ricky在这里。

要实现这里的目的,需要在代码中引入PHP异常处理。

  try {
    $account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
    echo $account->date_created;
  } catch (Exception $e) {
    echo $e->getMessage();
  }

如果凭据是正确的,这段代码将按照您的期望运行,但如果凭据不正确,错误将被捕获,我们将讨论与异常相关的消息。

希望有帮助!

我在使用Laravel 5.5和Twilio SDK时也遇到过类似的问题。

我得到Whoops'Handler'PrettyPageHandler handled error from 'vendor'twilio'sdk'Twilio'Version.php line 85 bla bla bla后试图捕捉异常,RestException

Andres Zapata的回答帮助我找到了正确的方向:

catch ('Twilio'Exceptions'RestException $e) {
     if ($e->getCode() == 20404) {
         //this will be false condition
         dd('False Result 404');
     } else {
         //some other exception code
         dd($e->getMessage());    
     }
}

上面的评论对我很有用,所以我想把它写下来作为一个正确的答案来帮助其他遇到这个问题的人。我在Services_Twilio_RestException中也缺少了前导反斜杠,所以我这样做了。

  try {
    $account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
    echo $account->date_created;
  } catch ('Services_Twilio_RestException $e) {
    echo $e->getMessage();
  }

在Services_Twilio_RestException中添加反斜杠

try{
}catch ('Services_Twilio_RestException $e){
 echo $e->getMessage();
}