twilio/sdk

A PHP wrapper for Twilio's API

Installs: 10 320 526

Dependents: 157

Suggesters: 9

Stars: 1 150

Watchers: 140

Forks: 449

Open Issues: 12

5.39.0 2019-11-13 20:23 UTC

README

Build 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.

Recent Update

On January 1, 2020, Twilio will no longer support PHP version 5 via this helper library. Please see this issue for details.

As of release 5.13.0, Beta and Developer Preview products are now exposed via the main twilio-php artifact. Releases of the alpha branch have been discontinued.

If you were using the alpha release line, you should be able to switch back to the normal release line without issue.

If you were using the normal release line, you should now see several new product lines that were historically hidden from you due to their Beta or Developer Preview status. Such products are explicitly documented as Beta/Developer Preview both in the Twilio docs and console, as well as through in-line code documentation here in the library.

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

Versions

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

Quickstart

Send an SMS

// Send an SMS using Twilio's REST API and PHP
<?php
$sid = "ACXXXXXX"; // Your Account SID from www.twilio.com/console
$token = "YYYYYY"; // Your Auth Token from www.twilio.com/console

$client = new Twilio\Rest\Client($sid, $token);
$message = $client->messages->create(
  '8881231234', // Text this number
  array(
    'from' => '9991231234', // From a valid Twilio number
    'body' => 'Hello from Twilio!'
  )
);

print $message->sid;

Make a Call

<?php
$sid = "ACXXXXXX"; // Your Account SID from www.twilio.com/console
$token = "YYYYYY"; // Your Auth Token from www.twilio.com/console

$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
  array(
      'url' => 'https://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient'
  )
);

Generating 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', array("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>

Prerequisites

  • PHP >= 7.1.32
  • The PHP JSON extension

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!