Skip to contentSkip to navigationSkip to topbar
On this page

Programmable Voice quickstart for PHP


This quickstart shows you how to build a PHP application that can make and receive phone calls. It uses Twilio's Voice API to make the outbound call and TwiML to respond to the inbound call by reading a message using text-to-speech.


Get a phone number

get-a-phone-number page anchor

Log in to your Twilio account(link takes you to an external page) or sign up for a new one(link takes you to an external page).

If you need a Twilio phone number, buy one in the Twilio Console:

  1. Go to Phone Numbers > Manage > Buy a Number(link takes you to an external page).
  2. Under Capabilities, select Voice.
  3. Click Search. The page displays a list of available voice-capable phone numbers.
  4. Click Buy next to a phone number to add it to your account.

Verify that your computer has PHP installed. Open a terminal and run the following command:

php --version

If your computer has PHP, then you should see a PHP version number like this:

PHP 8.4.7 (cli)

If you don't have PHP 7.2 or higher installed, then install or upgrade PHP(link takes you to an external page) before continuing.


Composer is a popular dependency manager for PHP. If you don't already have Composer installed on your computer, then install Composer for Windows(link takes you to an external page) or macOS(link takes you to an external page).


Install the Twilio PHP Helper Library

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

Create a new directory for this project and navigate to it using the command line. In your project directory, run the following command to install the Twilio PHP Helper Library:

You installed Composer globallyYou installed Composer locally
composer require twilio/sdk

Make an outgoing phone call with PHP

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

Create a new file called make_call.php with the following code:

1
<?php
2
require __DIR__ . '/vendor/autoload.php';
3
use Twilio\Rest\Client;
4
5
// To set up environment variables, see https://twil.io/secure
6
$account_sid = getenv('TWILIO_ACCOUNT_SID');
7
$auth_token = getenv('TWILIO_AUTH_TOKEN');
8
9
$client = new Client($account_sid, $auth_token);
10
11
// The recipient of the phone call
12
$to_number = "+15558675310";
13
14
// A Twilio number you own
15
$twilio_number = "+15017122661";
16
17
$call = $client->calls->create(
18
$to_number,
19
$twilio_number,
20
[
21
"url" => "http://demo.twilio.com/docs/voice.xml"
22
]
23
);

This code starts a phone call between the two phone numbers that you pass as arguments. The first argument to $client->calls->create() is the call recipient, and the second argument is your Twilio phone number.

The url option points to a TwiML file that tells Twilio what to do when the call recipient answers their phone. This TwiML instructs Twilio to read a message using text-to-speech and then play an MP3.

Replace the placeholder credential values

replace-the-placeholder-credential-values page anchor

Swap the placeholder values for $account_sid and $auth_token with your personal Twilio credentials.

  1. Log in to the Twilio Console(link takes you to an external page).
  2. Copy the Account SID and replace getenv('TWILIO_ACCOUNT_SID') with it in the make_call.php file.
  3. Copy the Auth Token and replace getenv('TWILIO_AUTH_TOKEN') with it.
(error)

Danger

This quickstart hardcodes your credentials to get you started quickly. Use environment variables and API keys to keep credentials secret and control access when you deploy to production.

Replace the to and from phone numbers

replace-the-to-and-from-phone-numbers page anchor

Replace the $twilio_number with your new Twilio phone number in E.164 format. Then, replace the $to_number with your own personal phone number in E.164 format.

Save your changes and then run the script using this command:

php make_call.php

Your phone should ring with a call from your Twilio number. Answer it, and you'll hear a short message.

(information)

Twilio trial account limitations

Twilio trial accounts limit outgoing phone calls to phone numbers you have verified with Twilio. To verify your phone numbers, use your Twilio Console's Verified Caller IDs(link takes you to an external page). To learn about other trial account limitations, see how to work with your free Twilio trial account.


Take a phone call with your Twilio number

take-a-phone-call-with-your-twilio-number page anchor

Create a file in your project directory called answer_call.php. Add the following code to it.

1
<?php
2
require __DIR__ . '/vendor/autoload.php';
3
use Twilio\TwiML\VoiceResponse;
4
5
// Start our TwiML response
6
$response = new VoiceResponse();
7
8
// Read a message aloud to the caller
9
$response->say(
10
"Thank you for calling! Have a great day.",
11
["voice" => "Polly.Amy"]
12
);
13
14
echo $response;

Save the file. Start your PHP development server with the following command:

php -S localhost:8000

You should see output similar to this:

[Thu Jul 3 15:06:06 2025] PHP 8.4.7 Development Server (http://localhost:8000) started

Visit http://localhost:8000/answer_call.php. You should see a TwiML document that says "Thank you for calling! Have a great day."


Expose your local server to the internet with ngrok

expose-your-local-server-to-the-internet-with-ngrok page anchor

If ngrok isn't installed on your computer, install it according to the instructions on ngrok's website(link takes you to an external page).

Open a new command line window and start ngrok with this command:

ngrok http 8000

You should see output that resembles this:

1
Session Status online
2
Version 3.23.2
3
Web Interface http://127.0.0.1:4040
4
Forwarding https://ff4660c91380.ngrok.app -> http://localhost:8000
5
6
Connections ttl opn rt1 rt5 p50 p90
7
0 0 0.00 0.00 0.00 0.00

Configure your Twilio webhook

configure-your-twilio-webhook page anchor
  1. Navigate to the Active Numbers(link takes you to an external page) page in the Twilio Console.
  2. Click on the phone number you purchased earlier.
  3. Visit the Configure tab and find the Voice Configuration section.
  4. In the A call comes in row, select the Webhook option. Enter your ngrok public URL in the URL field, appending /answer_call.php (for example, https://ff4660c91380.ngrok.app/answer_call.php).
  5. Click on Save configuration.

Head back to your terminal. Make sure that ngrok is still running in one tab and your PHP server is running in another tab.

You'll hear a short message once the call connects and it should match the TwiML instructions served by your PHP application.


Your new application uses the <Say> TwiML verb to read a message to the caller using text-to-speech. You can create more powerful constructs and call flows using other TwiML verbs. Try a few, such as <Record>, <Gather>, and <Conference>.

Check out these pages to learn more:

Let's build something amazing.