Björn C
Björn C

Reputation: 4008

503 Error while trying to send SMS with Twilio

I´m using Twilio PHP API to send mass sms to my customers.
Problem is that i can only send like 80st SMS and then i got a server error:

Failed to load resource: the server responded with a status of 503 (Backend fetch failed)

Well i think this might be the error.
Because i get no success echo echo "Sent message to $name and in the twilio SMS log i can only see that 80 SMS of 200 has been sent.

What can cause this error?

foreach ($usrs as $number => $name) {
    try{
        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                'from' => "xxxxxxxxxxx", 

                // the sms body
                'body' => "Hey $name. $text"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name <br>";
    } 
    catch (TwilioException $e) {
        die( $e->getCode() . ' : ' . $e->getMessage() );
    }
}

Upvotes: 1

Views: 672

Answers (1)

philnash
philnash

Reputation: 73057

Twilio developer evangelist here.

Twilio has a limit of 100 concurrent API requests and will only send 1 message a second. As Ahmed suggested in the comments, I recommend you add a delay between calls to the API if you are sending more than 100 messages.

edit

Adds sleep(1) for each message. This will make the page delay a second after sending each message.

foreach ($usrs as $number => $name) {
    try{
        $sms = $client->account->messages->create(

            // the number we are sending to - Any phone number
            $number,

            array(
                // Step 6: Change the 'From' number below to be a valid Twilio number 
                'from' => "xxxxxxxxxxx", 

                // the sms body
                'body' => "Hey $name. $text"
            )
        );

        // Display a confirmation message on the screen
        echo "Sent message to $name <br>";

        sleep(1);
    } 
    catch (TwilioException $e) {
        die( $e->getCode() . ' : ' . $e->getMessage() );
    }
}

Upvotes: 1

Related Questions