There are so many things that you can do in PHP, including creating websites, building command-line tools, generating images, encrypting and decrypting data, and scraping websites.
But did you know that PHP can also make phone calls? Okay, not on its own it can't, but with the help of Twilio Programmable Voice, it can! In this tutorial, you're going to learn how.
To follow along with this tutorial you will need the following:
- PHP 8.1.
- Composer globally installed.
- A Twilio account. If you are new to Twilio click here to create a free account.
- A Twilio phone number.
- A phone that can make and receive phone calls.
Set up your environment
The first thing you need to do is to create a project directory and change into it. To do that, run the following commands.
mkdir twilio-voice-call cd twilio-voice-call
Next, create a new file named .env to store the environment variables which the application requires. Then, in the new file paste the following code.
RECIPIENT_PHONE_NUMBER="<RECIPIENT_PHONE_NUMBER>" TWILIO_ACCOUNT_SID=<TWILIO_ACCOUNT_SID> TWILIO_AUTH_TOKEN=<TWILIO_AUTH_TOKEN> TWILIO_PHONE_NUMBER="<TWILIO_PHONE_NUMBER>" TWIML_BIN_URL="<TWIML_BIN_URL>"
Install the required dependencies
Next, you need to install the dependencies which the code requires. There are just two:
|The Twilio PHP Helper Library||Simplifies interacting with all of Twilio's APIs in PHP.|
|PHP Dotenv||Makes environment variables defined in `.env` available to PHP's `$_ENV` and `$_SERVER` superglobals.|
To install them, run the command below.
composer require twilio/sdk vlucas/phpdotenv
Set your Twilio credentials
The next thing you need to do is to retrieve your Twilio Auth Token, Account SID, and phone number. To do that login to the Twilio Console, copy the details from the "Account Info" panel and paste them in place of
<TWILIO_PHONE_NUMBER> respectively in .env.
Create a TwiML Bin
Now, you need to create the phone call instructions. These must be in TwiML format and available via a publicly accessible URL.
If you're not familiar with it, TwiML (the Twilio Markup Language) is a subset of XML which tells Twilio what to do, such as when you make or receive a phone call or SMS.
You don't need to host the file yourself, as you can host it for free in a TwiML Bin. To do that, navigate to the TwiML Bins section of the Twilio Console and click "Create new TwiML Bin".
Then, in the "FRIENDLY NAME" field, add a human-friendly name, such as
php-voice-call, and in the TwiML field add the TwiML below.
<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Ahoy, friend!</Say> </Response>
The TwiML instructions you used tell Twilio to say "Ahoy, friend" when the call is answered.
With that done, click "Create". With the TwiML Bin created, copy the URL and paste it into .env in place of
Make a Phone call in PHP
Now, it's time to write the code that will make the phone call. To do that, create a new PHP file named voice-call.php, and in the new file paste the following code.
<?php declare(strict_types=1); require_once './vendor/autoload.php'; use Twilio\Rest\Client; $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); $twilio = new Client( $_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN'] ); $call = $twilio->calls ->create( $_ENV['RECIPIENT_PHONE_NUMBER'], $_ENV['TWILIO_PHONE_NUMBER'], [ 'url' => $_ENV['TWIML_BIN_URL']] ); print($call->sid);
The code starts off by importing all the required libraries before using PHP Dotenv to load the variables defined in .env as variables in PHP's
After that, it initialises a new Twilio
Client object, which provides the functionality for interacting with Twilio's REST APIs. Then, via
$twilio->calls->create(), it makes a request to the Programmable Voice API to make a phone call, passing to the request:
- The recipient's phone number.
- The Twilio phone number to call from.
- The publicly accessible URL with the TwiML call instructions.
Assuming that the call was successful, the call's SID will be printed to the terminal and the phone call will be made.
Test the code
Now, it's time to test the code. To do that, run the command below.
Within a few seconds, you should receive a phone call on your mobile phone and hear "Ahoy, friend".
That's how you make phone calls using PHP and Twilio Programmable Voice
There's not a whole lot to the essentials of making a phone call in PHP Twilio Programmable Voice. To be fair, there is a lot more functionality that you can draw upon thanks to the incredible power of TwiML, but this is all you need to get started.
Do you prefer Python over PHP? Then, check out this post from Miguel Grinberg.
Otherwise, check out the TwiML documentation to learn more. I can't wait to see what you build!
Matthew Setter is a polyglot developer and the PHP Editor in the Twilio Voices team. He’s also the author of Deploy With Docker Compose, which shows the shortest path to deploying apps with Docker Compose. When he’s not writing code he’s editing great PHP articles here at Twilio. You can find him at email@example.com, and on Twitter and GitHub.
In this tutorial, you'll learn what RabbitMQ is and how to use It with Laravel
Learn how Twilio's Professional Services team can help you build an optimal, scalable, compliant Programmable Voice solution with our onboarding services.
In this tutorial, I'm going to show you how to import and retrieve SendGrid contacts using PHP
In this tutorial, I'm going to show you how to create one with PHP and then send it to a contact in a Twilio-powered SMS.
In this tutorial, you're going to learn how to create one with PHP and then send it to a contact using a Twilio-powered WhatsApp message.
In this tutorial, with PHP you'll learn how to send an SMS without a phone number by using an Alphanumeric Sender ID