我正在使用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& auth令牌在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()
答案 0 :(得分:1)
来自Twilio的Ricky。
要在此处完成您想要的操作,您需要在代码中引入PHP Exception handling。
try {
$account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
echo $account->date_created;
} catch (Exception $e) {
echo $e->getMessage();
}
如果凭据正确,则此代码将按预期运行,但如果凭据不正确,则会捕获错误,我们将了解与异常相关的消息。
希望有所帮助!
答案 1 :(得分:1)
我在Laravel 5.5和Twilio SDK上遇到了类似的问题。
我在尝试捕获Exception,RestException
后得到Whoops\Handler\PrettyPageHandler handled error from \vendor\twilio\sdk\Twilio\Version.php line 85 bla bla bla
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());
}
}
答案 2 :(得分:0)
上面的评论对我有用,所以我想把它写成一个正确的答案来帮助遇到这个问题的其他人。我也错过了Services_Twilio_RestException中的反斜杠,所以我就这样做了。
try {
$account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
echo $account->date_created;
} catch (\Services_Twilio_RestException $e) {
echo $e->getMessage();
}
答案 3 :(得分:0)
在Services_Twilio_RestException中添加反斜杠,如
try{
}catch (\Services_Twilio_RestException $e){
echo $e->getMessage();
}