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.
Log in to your Twilio account or sign up for a new one.
If you need a Twilio phone number, buy one in the Twilio Console:
- Go to Phone Numbers > Manage > Buy a Number.
- Under Capabilities, select Voice.
- Click Search. The page displays a list of available voice-capable phone numbers.
- 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 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 or macOS.
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:
composer require twilio/sdk
Create a new file called make_call.php
with the following code:
1<?php2require __DIR__ . '/vendor/autoload.php';3use Twilio\Rest\Client;45// To set up environment variables, see https://twil.io/secure6$account_sid = getenv('TWILIO_ACCOUNT_SID');7$auth_token = getenv('TWILIO_AUTH_TOKEN');89$client = new Client($account_sid, $auth_token);1011// The recipient of the phone call12$to_number = "+15558675310";1314// A Twilio number you own15$twilio_number = "+15017122661";1617$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.
Swap the placeholder values for $account_sid
and $auth_token
with your personal Twilio credentials.
- Log in to the Twilio Console.
- Copy the Account SID and replace
getenv('TWILIO_ACCOUNT_SID')
with it in themake_call.php
file. - Copy the Auth Token and replace
getenv('TWILIO_AUTH_TOKEN')
with it.
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 $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.
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. To learn about other trial account limitations, see how to work with your free Twilio trial account.
Create a file in your project directory called answer_call.php
. Add the following code to it.
1<?php2require __DIR__ . '/vendor/autoload.php';3use Twilio\TwiML\VoiceResponse;45// Start our TwiML response6$response = new VoiceResponse();78// Read a message aloud to the caller9$response->say(10"Thank you for calling! Have a great day.",11["voice" => "Polly.Amy"]12);1314echo $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."
If ngrok isn't installed on your computer, install it according to the instructions on ngrok's website.
Open a new command line window and start ngrok with this command:
ngrok http 8000
You should see output that resembles this:
1Session Status online2Version 3.23.23Web Interface http://127.0.0.1:40404Forwarding https://ff4660c91380.ngrok.app -> http://localhost:800056Connections ttl opn rt1 rt5 p50 p9070 0 0.00 0.00 0.00 0.00
- Navigate to the Active Numbers page in the Twilio Console.
- Click on the phone number you purchased earlier.
- Visit the Configure tab and find the Voice Configuration section.
- 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
). - 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:
- Gather user input via keypad (DTMF tones) in PHP
- Record incoming and outgoing Twilio Voice phone calls using PHP
- Create conference calls using PHP
- Read the API Reference documentation for Twilio Programmable Voice
- Learn how to modify calls in progress with PHP
- Use a Twilio Voice SDK to integrate high-quality VoIP calling into a web or mobile app
- Retrieve information about in-progress and completed calls from your Twilio account using PHP
- Check out our full sample application tutorial on how to transfer calls from one support agent to another with PHP and Laravel
Let's build something amazing.