-1

我正在使用 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()
4

4 回答 4

1

来自 Twilio 的 Ricky 在这里。

要在此处完成您想要的,您需要在代码中引入PHP 异常处理

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

如果凭据正确,则此代码将按您的预期运行,但如果凭据不正确,则会捕获该错误,我们将讨论与我们的异常相关的消息。

希望有帮助!

于 2015-10-03T16:57:48.100 回答
1

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());    
     }
}
于 2017-09-20T15:59:51.593 回答
0

在 Services_Twilio_RestException 中添加反斜杠,例如

try{
}catch (\Services_Twilio_RestException $e){
 echo $e->getMessage();
}
于 2016-08-03T15:07:53.497 回答
0

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

  try {
    $account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
    echo $account->date_created;
  } catch (\Services_Twilio_RestException $e) {
    echo $e->getMessage();
  }
于 2016-06-29T14:38:27.977 回答