Skip to content

A PHP library for communicating with the Twilio REST API and generating TwiML.

License

Notifications You must be signed in to change notification settings

twilio/twilio-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

twilio-php

Tests Quality Gate Status Packagist Packagist Learn with TwilioQuest

Documentation

The documentation for the Twilio API can be found here.

The PHP library documentation can be found here.

Versions

twilio-php uses a modified version of Semantic Versioning for all changes. See this document for details.

Supported PHP Versions

This library supports the following PHP implementations:

  • PHP 7.2
  • PHP 7.3
  • PHP 7.4
  • PHP 8.0
  • PHP 8.1
  • PHP 8.2
  • PHP 8.3

Installation

You can install twilio-php via composer or by downloading the source.

Via Composer

twilio-php is available on Packagist as the twilio/sdk package:

composer require twilio/sdk

Test your installation

Here is an example of using the SDK to send a text message:

// Send an SMS using Twilio's REST API and PHP
<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

// Your Account SID and Auth Token from console.twilio.com
$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

// Use the Client to make requests to the Twilio REST API
$client->messages->create(
    // The number you'd like to send the message to
    '+15558675309',
    [
        // A Twilio phone number you purchased at https://console.twilio.com
        'from' => '+15017250604',
        // The body of the text message you'd like to send
        'body' => "Hey Jenny! Good luck on the bar exam!"
    ]
);

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-main/src/Twilio/autoload.php';

// Your Account SID and Auth Token from console.twilio.com
$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

// Use the Client to make requests to the Twilio REST API
$client->messages->create(
    // The number you'd like to send the message to
    '+15558675309',
    [
        // A Twilio phone number you purchased at https://console.twilio.com
        'from' => '+15017250604',
        // The body of the text message you'd like to send
        'body' => "Hey Jenny! Good luck on the bar exam!"
    ]
);

Usage

Make a Call

<?php
$sid = "ACXXXXXX";
$token = "YYYYYY";

$client = new Twilio\Rest\Client($sid, $token);

// Read TwiML at this URL when a call connects (hold music)
$call = $client->calls->create(
    '8881231234',
    // Call this number
    '9991231234',
    // From a valid Twilio number
    [
        'url' => 'https://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient'
    ]
);

Warning It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out How to Set Environment Variables for more information.

Get an existing Call

<?php
require_once '/path/to/vendor/autoload.php';

$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

// Get an object using its SID. If you do not have a SID,
// check out the list resource examples on this page
$call = $client->calls("CA42ed11f93dc08b952027ffbc406d0868")->fetch();
print $call->to;

Iterate through records

The library automatically handles paging for you. Collections, such as calls and messages, have read and stream methods that page under the hood. With both read and stream, you can specify the number of records you want to receive (limit) and the maximum size you want each page fetch to be (pageSize). The library will then handle the task for you.

read eagerly fetches all records and returns them as a list, whereas stream returns an iterator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the page method.

<?php
require_once '/path/to/vendor/autoload.php';

$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

$limit = 5;
$pageSize = 2;

// Read - fetches all messages eagerly and returns as a list
$messageList = $client->messages->read([], $limit);
foreach ($messageList as $msg) {
    print($msg->sid);
}

// Stream - returns an iterator of 'pageSize' messages at a time and lazily retrieves pages until 'limit' messages
$messageStream = $client->messages->stream([], $limit, $pageSize);
foreach ($messageStream as $msg) {
    print($msg->sid);
}

// Page - get the a single page by passing pageSize, pageToken and pageNumber
$messagePage = $client->messages->page([], $pageSize);
$nextPageData = $messagePage->nextPage();  // this will return data of next page
foreach ($messagePage as $msg) {
    print($msg->sid);
}

For more information about these methods, view the auto-generated library docs.

Use the read method

<?php
require_once '/path/to/vendor/autoload.php';

$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

// Loop over the list of calls and print a property from each one
foreach ($client->calls->read() as $call) {
    print $call->direction;
}

Specify Region and/or Edge

To take advantage of Twilio's Global Infrastructure, specify the target Region and/or Edge for the client:

<?php
$sid = "ACXXXXXX";
$token = "YYYYYY";

$client = new Twilio\Rest\Client($sid, $token, null, 'au1');
$client->setEdge('sydney');

A Client constructor without these parameters will also look for TWILIO_REGION and TWILIO_EDGE variables inside the current environment.

This will result in the hostname transforming from api.twilio.com to api.sydney.au1.twilio.com.

Enable Debug Logging

There are two ways to enable debug logging in the default HTTP client. You can create an environment variable called TWILIO_LOG_LEVEL and set it to debug or you can set the log level to debug:

$sid = "ACXXXXXX";
$token = "YYYYYY";

$client = new Twilio\Rest\Client($sid, $token);
$client->setLogLevel('debug');

Generate TwiML

To control phone calls, your application needs to output TwiML.

Use Twilio\TwiML\(Voice|Messaging|Fax)Response to easily chain said responses.

<?php
$response = new Twilio\TwiML\VoiceResponse();
$response->say('Hello');
$response->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]);
print $response;

That will output XML that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Response>
    <Say>Hello</Say>
    <Play loop="5">https://api.twilio.com/cowbell.mp3</Play>
</Response>

Handle exceptions

When something goes wrong during client initialization, in an API request, or when creating TwiML, twilio-php will throw an appropriate exception. You should handle these exceptions to keep your application running and avoid unnecessary crashes.

The Twilio client

For example, it is possible to get an authentication exception when initiating your client, perhaps with the wrong credentials. This can be handled like so:

<?php
require_once('/path/to/twilio-php/Services/Twilio.php');

use Twilio\Exceptions\ConfigurationException;
use Twilio\Rest\Client;

$sid = "ACXXXXXX";
$token = "YYYYYY";

// Attempt to create a new Client, but your credentials may contain a typo
try {
    $client = new Twilio\Rest\Client($sid, $token);
} catch (ConfigurationException $e) {
    // You can `catch` the exception, and perform any recovery method of your choice
    print $e->getCode();
}

$call = $client->account->calls
    ->get("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

print $call->to;

CurlClient

When initializing the curl client, you will see an EnvironmentException if curl is not installed on your system.

<?php
require_once('/path/to/twilio-php/Services/Twilio.php');

use Twilio\Exceptions\TwilioException;
use Twilio\Http\CurlClient;

$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

try {
    $client = new CurlClient();

    $client->options(
        'GET',
        'http://api.twilio.com',
        array(),
        array(),
        array(),
        $sid,
        $token
    );
} catch (EnvironmentException $e) {
    print $e->getCode();
}

print $call->to;

TwilioException

TwilioException can be used to handle API errors, as shown below. This is the most common exception type that you will most likely use.

<?php
require_once('/path/to/twilio-php/Services/Twilio.php');

use Twilio\Exceptions\TwilioException;

$sid = "ACXXXXXX";
$token = "YYYYYY";
$client = new Twilio\Rest\Client($sid, $token);

try {
    $call = $client->account->calls
        ->get("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
} catch (TwilioException $e) {
    print $e->getCode();
}

print $call->to;

TwimlException

When building TwiML with twilio-php, if the result does not conform to what the API expects, you will see a TwimlException which you then need to handle like so:

<?php
require_once './vendor/autoload.php';
use Twilio\Twiml;

try {
    $response = new Twiml();
    $dial = $response->dial();
    $dial->conference('Room 1234');
    print $response;
} catch (TwimlException $e) {
    print $e->getCode();
}

Debug API requests

To assist with debugging, the library allows you to access the underlying request and response objects. This capability is built into the default Curl client that ships with the library.

For example, you can retrieve the status code of the last response like so:

<?php
$sid = "ACXXXXXX";
$token = "YYYYYY";

$client = new Twilio\Rest\Client($sid, $token);
$message = $client->messages->create(
    '+15558675309',
    [
        'from' => '+15017250604',
        'body' => "Hey Jenny! Good luck on the bar exam!"
    ]
);

// Print the message's SID
print $message->sid;

// Print details about the last request
print $client->lastRequest->method;
print $client->lastRequest->url;
print $client->lastRequest->auth;
print $client->lastRequest->params;
print $client->lastRequest->headers;
print $client->lastRequest->data;

// Print details about the last response
print $client->lastResponse->statusCode;
print $client->lastResponse->body;

Use a custom HTTP Client

To use a custom HTTP client with this helper library, please see the advanced example of how to do so.

Docker image

The Dockerfile present in this repository and its respective twilio/twilio-php Docker image are currently used by Twilio for testing purposes only.

Getting help

If you need help installing or using the library, please check the Twilio Support Help Center first, and file a support ticket if you don't find an answer to your question.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!