Twilio blacklist rule fatal error

Twilio developer evangelist here.

You need to catch the exception that is thrown from the request to send a message to the blacklisted number. You can do so with try and catch like this:

foreach ($people as $number => $name) {
    try {
        $sms = $client->account->messages->create(
            $number,
            array(
                'from' => "+18443949780", 
                'body' => "Hey $name, this is Hussam. Testing Twilio SMS API!"
            )
        );
        echo "Sent message to $name.\n";
    } catch (\Twilio\Exceptions\RestException $e) {
        echo "Couldn't send message to $number\n";
    }
}

When you hook this up to a database, you'll want to use the catch to update a field to mark the number as blocked so that you don't try to send to it again.

Let me know if that helps at all.


This worked for me with Laravel 5. Notice the use of \Twilio\Exceptions\RestException.

   try {
        $sms = $client->account->messages->create(
            $number,
            array(
                'from' => "+16136543180", 
                'body' => "Hey $name, Are you still mad at us about your cat!"
            )
        );
        echo "Sent message to $name.\n";

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