I am using Twilio to send SMS to users. On an invalid number, I am getting 400. How can I catch that exception or HTTP Status code so that I can handle it accordingly? It does return these codes and I want to know the HTTP Code received in the API response.
$client = new Client($account_sid, $auth_token);
$client->messages->create(
$to, array(
'body' => $message,
'from' =>$SMS_FROM
)
);
Why not just wrap the code block in a try/catch block, as in this example?
try {
$twilio
->messages
->create(
$to,
[
'body' => "Here is my message to you!",
'from' => $SMS_FROM,
]
);
} catch (\Twilio\Exceptions\TwilioException $e) {
echo "Something went wrong.\nCode: {$e->getCode()}.\nMessage: {$e->getMessage()}.\n";
}
This will print out the full error message which should give you what you want, e.g.:
[HTTP 400] Unable to create record: The 'From' number +12132635abc137 is not a valid phone number, shortcode, or alphanumeric sender ID.