Get Started

Install the Twilio PHP Helper Library

The twilio-php helper library lets you write PHP code to make HTTP requests to the Twilio API. You can install the twilio-php library in one of three different ways.

Install via PEAR (>= 1.9.3)

PEAR is a package manager for PHP. Open a command line and use these PEAR commands to download the helper library:

$ pear channel-discover twilio.github.com/pear
$ pear install twilio/Services_Twilio

If you get the following message:

-bash: pear: command not found

you can install PEAR from their website, or see the instructions below to download twilio-php library via a ZIP file.

"Permission Denied"

If the command line gives you a big long error message that says Permission Denied in the middle of it, try running the above commands with sudo, ex. sudo pear install twilio/Services_Twilio.

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!

Install via Composer

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

Packagist is an online repository of PHP packages. The twilio-php library is available on Packagist as the twilio/sdk package. Add "twilio/sdk" to the composer.json file in the root of your project, and then run:

composer install

or:

php composer.phar install

The exact command depends on how you've installed Composer - see their documentation for more details.

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

    $sms = $client->account->sms_messages->create(
        "YYY-YYY-YYYY", // From this number
        "XXX-XXX-XXXX", // To this number
        "Test message!"
    );

    // Display a confirmation message on the screen
    echo "Sent message {$sms->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.

More Documentation

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. If you'd like to learn more about how Twilio works, or for inspiration about what to build, try the PHP quickstarts.

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 {
    $sms = $client->account->sms_messages->create(
        "YYY-YYY-YYYY", // From this number
        "XXX-XXX-XXXX", // To this number
        "Test message!"
    );
} catch (Services_Twilio_RestException $e) {
    echo $e->getMessage();
}

Getting help

Still running into problems?