Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Programmable Voice Quickstart for PHP


With just a few lines of code, your PHP web application can make and receive phone calls with Twilio Programmable Voice.

This PHP Voice quickstart will teach you to do this using Twilio's Voice REST API, the Twilio PHP helper library(link takes you to an external page), the built-in PHP development web server, and ngrok(link takes you to an external page) to expose your local server to Twilio.

We'll use the Composer package manager for dependency management (don't want to? Try our Non Composer PHP Voice Quickstart).

In this quickstart, you will learn how to:

  1. Sign up for Twilio and get your first voice-enabled Twilio phone number
  2. Check and install any PHP prerequisites using Composer(link takes you to an external page)
  3. Make an outbound phone call and play an MP3
  4. Receive and respond to an inbound phone call to read a message to a caller using Text to Speech

Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with PHP(link takes you to an external page).


Sign up for Twilio and get a phone number

sign-up-for-twilio-and-get-a-phone-number page anchor
(information)

Info

Already have a Twilio account and a voice-enabled Twilio phone number? Log in(link takes you to an external page) then jump to the next step.

You can sign up for a free Twilio trial account here(link takes you to an external page).

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to make calls to your phone from your Twilio account while in trial mode.
  • Once you verify your number, you'll be asked to create a project. For the sake of this tutorial, you can click on the "Learn and Explore" template. Give your project a name, or just click "skip remaining steps" to continue with the default.
  • Once you get through the project creation flow, you'll arrive at your project dashboard in the Twilio Console(link takes you to an external page) . This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.

If you don't currently own a Twilio phone number with Voice functionality, you'll need to purchase one. After navigating to the Buy a Number(link takes you to an external page) page, check the "Voice" box and click "Search."

Search for a Phone Number.

_10
php --version

If PHP is not installed, follow the PHP installation instructions(link takes you to an external page).

If you're using a Windows machine, follow the official PHP tutorial to install PHP(link takes you to an external page).

(error)

Danger

While many versions of PHP 7.x will work for this quickstart, please pay attention to supported PHP releases(link takes you to an external page).When doing web development, always update your PHP version to a release which receives security updates.

Install Composer for package management

install-composer-for-package-management page anchor

Composer is the de facto canonical package manager for PHP web development. For this tutorial, Composer is required. If you don't want to install Composer, try our non Composer PHP Voice Quickstart.

Install the Twilio PHP Helper Library

install-the-twilio-php-helper-library page anchor

Now we need to install the Twilio PHP Helper Library.

First, navigate to the directory where you will complete this quickstart then choose one of the following methods to install the helper library.

From a terminal, you can run the following command:


_10
composer require twilio/sdk

Or, you can create a file named composer.json instead. In that file, add:


_10
{
_10
"require": {
_10
"twilio/sdk": "^5.0"
_10
}
_10
}

Next, run


_10
composer install

Composer will grab the latest 5.x version of the Twilio PHP Helper Library.

To not use Composer, try this PHP Voice Quickstart.


Make an outgoing phone call with PHP

make-an-outgoing-phone-call-with-php page anchor

Now that we have PHP and the PHP Helper Library installed, we can make an outgoing phone call in a single API request. Create a new file called make_call.php and type or paste in this code sample.

Make a voice call

make-a-voice-call page anchor

Make a Voice call with the PHP Helper Library


_25
<?php
_25
require __DIR__ . '/vendor/autoload.php';
_25
use Twilio\Rest\Client;
_25
_25
// Your Account SID and Auth Token from twilio.com/console
_25
// To set up environmental variables, see http://twil.io/secure
_25
$account_sid = getenv('TWILIO_ACCOUNT_SID');
_25
$auth_token = getenv('TWILIO_AUTH_TOKEN');
_25
// In production, these should be environment variables. E.g.:
_25
// $auth_token = $_ENV["TWILIO_ACCOUNT_SID"]
_25
_25
// A Twilio number you own with Voice capabilities
_25
$twilio_number = "+15017122661";
_25
_25
// Where to make a voice call (your cell phone?)
_25
$to_number = "+15558675310";
_25
_25
$client = new Client($account_sid, $auth_token);
_25
$client->account->calls->create(
_25
$to_number,
_25
$twilio_number,
_25
array(
_25
"url" => "http://demo.twilio.com/docs/voice.xml"
_25
)
_25
);

When you run the code, it will start a phone call between the two numbers passed as arguments. We've added two variables to show you the 'From' and 'To' number and they will map like this:

  • (From) / twilio_number : The Twilio phone number you purchased
  • (To) / to_number : The number to call (perhaps your cell phone?)

Inside the array, the url argument points to some TwiML. TwiML is a language Twilio uses internally to initiate or respond to actions, such as a sequence for a voice call. This particular TwiML causes Twilio to read a message with text to speech then play an MP3 to the 'To' / to_number number.

Before you can run this code, you need to swap in a couple of account specific values.

Replace the placeholder credentials with your own

replace-the-placeholder-credentials-with-your-own page anchor

Substitute the placeholder values for account_sid and auth_token with your Twilio account credentials.

To find these, visit https://www.twilio.com/console(link takes you to an external page) and log in. On this page, you'll find your unique Account SID and Auth Token. You can reveal the Auth Token by clicking the 'eye' icon:

Reveal Your Auth Token.Rate this page:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.