The Twilio PHP Helper Library

The Twilio PHP SDK makes it easy to interact with the Twilio API from your PHP application. The most recent version of the Twilio PHP SDK can be found on Packagist. The Twilio SDK requires PHP version 5.3 or higher. If you are interested in migrating to the newer 5.x version of the Twilio PHP SDK from the 4.x version, check out this guide.

Using with Composer

The recommended method for installing the SDK is via Composer. You can add the PHP SDK to your composer.json file with the require command.

composer require twilio/sdk

If you are using a framework like Laravel, the Twilio SDK may be automatically loaded for you and ready to use in your application. If you're using Composer in an environment that doesn't handle autoloading, you can require the autoload file from the "vendor" directory created by Composer if you used the install command above. Here is a basic example of using the SDK to send a text message.

<?php
// Required if your envrionment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    '+15558675309',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => 'Hey Jenny! Good luck on the bar exam!'
    )
);

Using without Composer

While we recommend using a package manager to track the dependencies in your application, it is possible to download and use the PHP SDK manually. You can download the full source of the PHP SDK from GitHub, and browse the repo if you would like. To use the SDK in your application, unzip the SDK download file in the same directory as your PHP code. In your code, you can then require the autoload file bundled with the SDK.

<?php
// Require the bundled autoload file - the path may need to change
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$token = 'your_auth_token';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    '+15558675309',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => "Hey Jenny! Good luck on the bar exam!"
    )
);

Accessing the 4.x Version of the SDK

The most recent version of the PHP SDK is not API compatible with the previous 4.x version of the PHP SDK you may have used in previous Twilio applications. The older version will continue to work, and you will continue to find sample code for this version throughout our documentation. Should you need to install this version from Composer, you can do so with the following command.

composer require twilio/sdk:4.11.0

Alternately, you can view this version of the SDK on GitHub and download it directly from there.

Accessing Preview Twilio Features

Pre-release products are only available on the "alpha" release of the PHP SDK (version 5.x or higher). You can read more about the versioning strategy for the SDK here. Whenever a new version of the SDK is released, a corresponding "alpha" version will also be released and installable. For example, if the current production version of the SDK were "5.0.2", you could install the "alpha" version with the following command.

composer require twilio/sdk:5.0.2-alpha

Getting Help

We'd love to hear your feedback on the PHP SDK, and help you past any issues you may encounter. Feel free to drop us a line, and we'll make sure to get you sorted!