Twilio catching error not working

I use twilio in my laravel 5 application. To use it within the framework, I use the aloha/laravel-twilio .

Sending a valid test-credentials request works fine. I have problems when I want to implement error handling .

For some reason, the catch does not receive an error, which causes the application to crash. The error seems to be in twilio-sdk if I read the error message correctly.

Here is what I have done so far:

 <?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 } } } 

This results in the following error:

 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. 

This error should be selected from the documentation (not a valid phone number), but I should be able to catch and process it. This is currently not working. I am not getting an error ...

How to get twilio errors received and processed?

+1
php try-catch laravel-5 twilio twilio-php
Aug 16 '15 at 10:53 on
source share
3 answers

The class is in the namespace, so I need to refer to the class exception absolut - \Services_Twilio_RestException - in catch.

It works with this code:

  try { \Twilio::message( $toNumber, 'Pink Elephants and Happy Rainbows'); } catch ( \Services_Twilio_RestException $e ) { elog( 'EACT', $e->getMessage( ) , __FUNCTION__ ); } 
+11
Aug 17 '15 at 23:08
source share
— -

Today (May 19, 2017) the code is as follows:

  // 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() ); } 
+1
May 19 '17 at 11:14
source share

See below, which is valid for today. TwilioException invalid and not equal to Services_Twilio_RestException . Use Exception instead.

My use case I had to send to the database of numbers and not have the wrong phone number, break my script. We worked about a month or two ago, which is related to registering messages when a message was sent, and conducted a cron check where we stopped every two minutes ... ineffectively when you send tens of thousands of text messages.

 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); 
+1
Aug 25 '17 at 3:34 on
source share



All Articles