The Twilio PHP Helper Library

The twilio-php helper library lets you write PHP code to make HTTP requests to the Twilio API.

This library is open source, so if you find a feature missing or a bug, we encourage you to contribute back to the twilio-php project hosted on Github.

More Documentation

If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the PHP quickstarts.

The Rest API Documentation has examples written in PHP for every kind of action you could take with the Twilio API. You should start there if you know what you want to do with the PHP library.

You may also want to refer to the open source twilio-php specific documentation for more details about the functions and classes contained in the library.

Installation

There are two ways to install the twilio-php library: using Composer or downloading the source.

Install via Composer

Composer is a package manager for PHP, similar to RubyGems or pip for Python. Here are instructions for installing Composer on your machine.

Once Composer is installed, adding the twilio-php project is as easy as:

composer require twilio/sdk

If that doesn't work for you, check out the Composer documentation for specifying dependencies.

Install via ZIP file

One easy way to get started with the library is to simply download all of the files and manually add them to your project. This may be the best option if you are using a shared hosting service such as GoDaddy, where you don't have SSH access into the machine, or you cannot run commands at a command line.

Click here to download the twilio-php source code (.zip) which includes all dependencies.

Once you download the library, move the twilio-php folder to your project directory and then include the library file:

require '/path/to/twilio-php/Services/Twilio.php';

and you're good to go!

Testing your installation

Try sending yourself an SMS message, like this:

send-sms.php
    <?php

    require "/path/to/twilio-php/Services/Twilio.php";

    // set your AccountSid and AuthToken from www.twilio.com/user/account
    $AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";

    $client = new Services_Twilio($AccountSid, $AuthToken);

    $message = $client->account->messages->create(array(
        "From" => "YYY-YYY-YYYY",
        "To" => "XXX-XXX-XXXX",
        "Body" => "Test message!",
    ));

    // Display a confirmation message on the screen
    echo "Sent message {$message->sid}";

You can run that file 3 different ways:

  • Save it as send-sms.php and at the command line, run php send-sms.php
  • Upload it to a web host and load mywebhost.com/send-sms.php in a web browser.
  • Download a local server like WAMP, MAMP or XAMPP. Point the web root directory to the folder containing this file, and load localhost:8888/send-sms.php in a web browser.

Using the Library

Try the PHP quickstarts and refer to the open source twilio-php specific documentation for details about the functions and classes contained in the library.

Handling Exceptions

If the Twilio API returns a 400 or a 500 level HTTP response, the twilio-php library will throw a Services_Twilio_RestException. 400-level errors are normal during API operation ("Invalid number", "Cannot deliver SMS to that number", for example) and should be handled appropriately. Below is an example of how to handle exceptions from an API request.

<?php

require "/path/to/twilio-php/Services/Twilio.php";

// set your AccountSid and AuthToken from www.twilio.com/user/account
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";

$client = new Services_Twilio($AccountSid, $AuthToken);

try {
    $message = $client->account->messages->create(array(
        "From" => "YYY-YYY-YYYY",
        "To" => "XXX-XXX-XXXX",
        "Body" => "Test message!",
    ));
} catch (Services_Twilio_RestException $e) {
    echo $e->getMessage();
}

Getting Help

Still running into problems?