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, the built-in PHP development web server, and ngrok 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:
Prefer to get started by watching a video? Check out our video on how to place and receive phone calls with PHP.
Already have a Twilio account and a voice-enabled Twilio phone number? Log in then jump to the next step.
You can sign up for a free Twilio trial account here.
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 page, check the "Voice" box and click "Search."
_10php --version
If PHP is not installed, follow the PHP installation instructions.
If you're using a Windows machine, follow the official PHP tutorial to install PHP.
While many versions of PHP 7.x will work for this quickstart, please pay attention to supported PHP releases.When doing web development, always update your PHP version to a release which receives security updates.
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.
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:
_10composer 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
_10composer install
Composer will grab the latest 5.x version of the Twilio PHP Helper Library.
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 with the PHP Helper Library
_25<?php_25require __DIR__ . '/vendor/autoload.php';_25use 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:
twilio_number
: The Twilio phone number you purchased
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.
Substitute the placeholder values for account_sid
and auth_token
with your Twilio account credentials.
To find these, visit https://www.twilio.com/console 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: