-1

i am using twilio php api to get date created 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;

when i enter wrong account sid & auth token its show a error message on twilio.php file how to get this error on my page.

Error message:-

  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()
Maksud Mansuri
  • 631
  • 13
  • 26
Mukesh Kumar
  • 172
  • 9

4 Answers4

1

Ricky from Twilio here.

To accomplish what you want here you'll want to introduce PHP Exception handling into your code.

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

If the credentials are correct this code will run as you expect but if the credentials are incorrect that error will be caught and we'll about the message related to our exception.

Hope that helps!

rickyrobinett
  • 1,224
  • 10
  • 13
  • @rickyrobinett I'm getting the same exception, but the cause is there's no credit on my clients account. I'm trying to catch it to produce a friendly message for the user, but it doesn't work. try { $this->connection->account->messages->sendMessage($this->from, $to, $message); } catch (Services_Twilio_RestException $e) { echo 'Twilio Exception'. $e->getMessage(); } – Andres Zapata Nov 12 '15 at 14:58
  • 1
    I was missing the leading backslash in Services_Twilio_RestException. – Andres Zapata Nov 12 '15 at 15:04
1

I had similar issue with Laravel 5.5 and Twilio SDK.

I was getting Whoops\Handler\PrettyPageHandler handled error from \vendor\twilio\sdk\Twilio\Version.php line 85 bla bla bla after trying to catch Exception, RestException

The answer from Andres Zapata helped me go in the right direction:

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

The comment above worked for me so I wanted to write it as a proper answer to help anyone else that comes across this issue. I too was missing the leading backslash in Services_Twilio_RestException, so I made it like this.

  try {
    $account = $client->accounts->get("ACa89d3917a5b56ebccd*********");
    echo $account->date_created;
  } catch (\Services_Twilio_RestException $e) {
    echo $e->getMessage();
  }
stuyam
  • 9,561
  • 2
  • 21
  • 40
0

add backslash in Services_Twilio_RestException like

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