Twilio Tutorial: Sending an SMS with the PHP REST API

Note: this post was previewed and approved by the customer.

Twilio Logo
Twilio Logo

Update: One day after publishing this post, Kevin from Twilio suggests in the comments to use the last version from Github instead the 3.2.3 twilio-php library zip archive from the Twilio page. That fixed an SSL certificate validation issue. This is what we call great support!

In a recent project, we used the Twilio REST API to send SMS messages. Here is a very simple tutorial about it.

If you need help integrating Twilio in your application, you can contact us.

1. Register your phone number with Twilio (or get  a production account).

At the time of writing this post, Twilio offers a trial account with $30 free credit. Just go to the Twilio site and create a new one. The trial account requires verification of the phone numbers you want to use with their service. You can do this, at no extra cost, by going to Numbers → Verify a number in their control panel. You will then receive a call to that number asking for confirmation.

For your caller ID, you can use the sandbox number you will find after logging in to, buy a number from them or use a verified number. Alternatively, you can upgrade your account to a production one, which will allow you to use any phone number (you have to register a credit card with them, but you don’t need to make a payment until you spend the initial $30 credit).

2. The configuration file

Now, download the twilio-php library from their site (I used version 3.2.3 for this tutorial), or get it from Github  and install it in your server (I used the twilio-lib directory). In the root of your site (or in your test dir) create a twilio.config.php file with the following:


$AccountSid = "Your Account SID";
$AuthToken = "Your Auth Token";

Replace the values with the ones you will find after login into Twilio.

3. Send an SMS.

This is pretty straightforward:


try {
    // Instantiate a new Twilio Rest Client
    $client = new Services_Twilio($AccountSid, $AuthToken);

     * If you get an "Services_Twilio_TinyHttpException: SSL certificate problem [...]";
     * exception, uncomment the two lines below. More about this issue at
     * UPDATE: using a twilio-php library version newer than 3.2.3 or the last version
     * from Github fixes this issue, making the two lines below unnecessary.

     // $http = new Services_Twilio_TinyHttp("", array("curlopts" => array(CURLOPT_SSL_VERIFYPEER => false)));
     // $client = new Services_Twilio($AccountSid, $AuthToken, "2010-04-01", $http);

     * The number we are sending from, it must be a valid Twilio number
     * You can use the default number which you will find in the sandbox,
     * buy a number from them or port your own number to Twilio.
    $from = "415-599-2671"; // this is the default sandbox number
    // The number we are sending to - any phone number
    $to = "A Phone Number";
    // The SMS body
    $message = "Testing the Twilio SMS REST API";

    // Send a new outgoing SMS
    $response = $client->account->sms_messages->create($from, $to, $message);

    // Get the delivery status
    echo $response->status;
} catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();

4. Check the results.

If everything went well, you will receive an SMS message in your phone. You can also go to Logs → SMS in the Twilio site and see the results there:

Twilio SMS Logs
Twilio SMS Logs Screen

Any questions? Feel free to post a comment.


3 thoughts on “Twilio Tutorial: Sending an SMS with the PHP REST API

  1. Hi Sergio,
    Great tutorial. I am worried you are still getting cURL exceptions with the twilio-php library. We recently began shipping the public key for with the helper library, to get around this problem. Can you download the latest version and see if you still get cURL exceptions?


  2. very informatics and energetic blog… thanks for sharing with us this beautiful information……..


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s