With very 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 install and manage packages manually in this quickstart
We highly suggest trying Composer for package and dependency management in your PHP web applications. If interested, try our standard PHP Voice Quickstart.
In this quickstart, you will learn how to:
- Sign up for Twilio and get your first voice-enabled Twilio phone number
- Check and install any PHP prerequisites manually
- Make an outbound phone call and play an MP3
- Receive and respond to an inbound phone call to read a message to a caller using Text to Speech
You can sign up for a free Twilio trial account here.
- 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. 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 page, check the "Voice" box and click "Search."
You’ll then see a list of available phone numbers and their capabilities. Find a number that suits your fancy and click "Buy" to add it to your account.
If you already have PHP and the Twilio PHP Helper Library installed locally, you're all set here. Feel free to skip ahead and move on to sending your first text message.
To make your first outbound voice call, let's make sure you're set up with PHP and able to install Twilio's PHP Helper library.
In this quickstart, we'll install the Twilio Helper Library manually. When doing web development in PHP, we strongly suggest using Composer for your package management. If you'd prefer to do that, try our standard PHP Voice Quickstart.
If you’re using a Mac or *nix machine, you may already have PHP installed. In your terminal, run:
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.
Many versions of PHP 5.x and PHP 7.x will work for this quickstart, but please pay attention to supported PHP releases. When doing web development, always update PHP to a release which receives security updates.
Now we need to install the Twilio PHP Helper Library.
- Create a new project directory or navigate to your project's working directory.
- In that directory, download the most recent version of the Twilio PHP Helper Library from Github.
- Unzip the PHP helper library in the directory.
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.
When you run the code, it will start a phone call between the two numbers included in this code. We added two variables to show you the 'From' and 'To' number. The numbers 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 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' /
Before you can run this code, you need to swap in a few account-specific values.
Substitute the placeholder values for
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 'view':
make_call.php and replace the values for
auth_token with your unique values.
Note: It's easier to hardcode your credentials in a quickstart, but use environment variables to keep them secret in production. Check out how to set environment variables for more information and see the code comments for a reading example. This repo is also an excellent reference for environment variables in PHP.
[+][country code][phone number including area code]
Again using E.164 formatting, substitute the phone number which will receive the outgoing call. You should use a personal phone number here so you'll receive the call and hear the magic after running the code.
Save the script and invoke it to test an outgoing call. In OSX and *NIX, this looks like:
If you substituted everything correctly, you should hear a great message and a brand new song! If it didn't work though, don't ever give up and follow the console prompts.
When someone calls your Twilio number, Twilio makes an HTTP request to your server to ask how to handle the call. For this quickstart, we’ll reply to the sender and thank them for their phone call using Twilio's text to speech capabilities.
Again, we'll use the Twilio PHP Library for this step. We will use PHP's built-in development webserver in combination with ngrok to instruct Twilio on how to manage the call.
Create a new file,
answer_call.php, in the same directory as
make_call.php. Then copy and paste or type the following code.
(Note: if you do not use the same directory as you did when we made our first call, please follow the PHP Helper Library install step above)
answer_call.php, then start a local PHP development server:
php -S localhost:8000
In a browser tab open http://localhost:8000/answer_call.php.
If everything went well, you should see XML in your browser with the message we'd like to speak to incoming calls. That's all the code you need - there are just a few more steps before everything is wired up.
Next, let's expose this endpoint to Twilio.
Before we can instruct Twilio on how to handle an incoming call, we first need to expose the server to the public. When you run your local development server, the odds are very high it is only accessible locally. But don't worry - we'll show you an easy way to test your server.
Many Twilio products and services use webhooks to communicate with your application. For example, when Twilio receives an incoming call, it reaches out to a URL you provide for instructions on how to handle the response. The small piece of code in
answer_call.php is an example of one instruction you can use to 'speak' back to the caller. However, when you run the server, it will only be exposed to you locally, not to the broader public. We need to figure out a way to fix that.
While there are many ways to make this code public (for example using an external host), we recommend a tool called ngrok. When you start ngrok, it provides a unique URL on the ngrok.io domain and forwards incoming requests to your local development environment.
The architecture looks like this:
If you don’t already use ngrok, visit the download page and install it for your operating system.
If you're working on Mac or Linux, you're all set after you decompress it. If you're on Windows, follow our guide on how to install and configure ngrok on Windows. For more info on ngrok (including some great tips and tricks) check out our this ngrok blog post.
Once you download an install ngrok, open a new terminal tab or window (leaving your development server running) and start it with this command:
./ngrok http 8000
You should see output similar to this:
Copy the public URL from this output, paste it into your browser, and append
answer_call.php. You should see the same XML you saw previously, except now you can access this from anywhere you've got internet access.
Now that you're public let's tell Twilio where to find the server.
Now that your server is publically accessible you can configure your Twilio phone number to access your webhook URL.
- Visit the Console Numbers page.
- Click on your voice-enabled phone number (from before).
- Find the Voice & Fax section. Make sure the "Accept Incoming" selection is set to "Voice Calls." The default "Configure With" selection is what you’ll need: "Webhooks/TwiML...".
- In the A Call Comes In section, select "Webhook" and paste in the URL you want to use, appending
Save your changes. You're ready to roll!
If your local development server is still running and ngrok is still going, you're ready for the fun part - testing!
Make a phone call to your Twilio phone number. You'll see and hear several things happen in short succession:
- You'll see an HTTP request in the ngrok console
- The development server will print out some messages to the console
- You'll hear the message once the call connects
And there you go - outbound and inbound voice calling with PHP.
Now you know the basics of making and responding to phone calls with PHP.
This PHP app only used the <Say> TwiML verb to read a message to the caller using text to speech. With different TwiML verbs, you can create other powerful constructs and call flows. 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
- How to 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 Twilio Client to integrate high-quality VoIP calling to 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
We can't wait to see what you build!