Set up your PHP development environment
In this guide, you will learn how to set up your PHP development environment for building applications with Twilio. By following this guide, you will install PHP, set up Composer for dependency management, install the Twilio PHP SDK, and configure ngrok to expose your local development environment to the internet.
Before you begin this setup guide, make sure you have:
- Administrative access to your computer to install software.
- A stable internet connection for downloading packages and dependencies.
- Basic familiarity with using the command line/terminal.
- A Twilio account (free tier available).
Time to complete: Approximately 15-30 minutes depending on your operating system and internet speed.
How you install PHP varies depending on your operating system.
Operating system | Instructions |
---|---|
macOS | The easiest way to install PHP on macOS is to use the official installer from php.net. You can also use Homebrew if you prefer: brew install php . |
Windows | To install PHP on Windows, use the official installer from php.net. You can also use Chocolatey if you prefer: choco install php . |
Linux | The exact instructions to install PHP vary by distribution. Find instructions for Ubuntu or Debian. |
Before you start your PHP project, you'll need a text editor or Integrated Development Environment (IDE).
If you already have a preferred text editor or IDE, you can use it for developing your PHP application. If you're looking for something new, you should try out a few options:
- Visual Studio Code is a popular code editor with integrated support for PHP through extensions.
- Vim is a perennial favorite text editor among advanced users.
- Emacs is a highly configurable editor that can be extended via Lisp.
Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on, and it will manage them for you.
Note: Composer requires PHP to be installed first. Make sure you've completed the Install PHP section above before proceeding.
Before you can use Composer, you'll need to install it on your system:
Operating system | Instructions |
---|---|
macOS | Recommended: Download and install from getcomposer.org using the installer script: curl -sS https://getcomposer.org/installer \\| php -- --install-dir=/usr/local/bin --filename=composer . Alternative: Use Homebrew if you have it working: brew install composer . |
Windows | Download the installer from getcomposer.org and run the setup wizard. |
Linux | Download and install using the command line instructions at getcomposer.org, or use your distribution's package manager. |
To verify you installed Composer correctly, run:
composer --version
You will see output similar to Composer version 2.x.x
.
If you encounter issues with Homebrew (such as download failures or missing dependencies), use the direct installation method instead.
Note: Make sure PHP is installed first (see the Install PHP section above) before running these commands.
- Download and run the Composer installer:
curl -sS https://getcomposer.org/installer | php
- Move Composer to a directory in your PATH:
sudo mv composer.phar /usr/local/bin/composer
- Make it executable:
sudo chmod +x /usr/local/bin/composer
- Verify the installation:
composer --version
To start a new project with Composer:
- Create a new empty directory in your development environment:
1mkdir twilio-php-app2cd twilio-php-app
- Initialize the project:
composer init
Follow the prompts to complete the initialization:
- Package name: Enter in
vendor/project
format (e.g.,yourname/twilio-php-app
). - Description: Brief project description (optional).
- Dependencies: Skip for now as you will add them next.
- Autoloading: Accept PSR-4 mapping to
src/
directory.
Composer will create a new composer.json
file when you're done.
Note: You must follow the prompts to set up your composer.json
file. This step is required to continue.
You're almost ready to start writing your PHP application, but first, you need to install the Twilio PHP SDK.
Install the Twilio SDK using Composer:
composer require twilio/sdk
You can test that your development environment has been configured correctly by creating a basic PHP application.
Create a new PHP file named index.php
with the following content:
1<?php2require_once "vendor/autoload.php";34use Twilio\TwiML\VoiceResponse;56$response = new VoiceResponse();7$response->say("Hello World!");89header("Content-Type: text/xml");10echo $response;
Run your PHP web application with the command:
php -S localhost:3000
You can then open http://localhost:3000/index.php
in your browser and see the <Response><Say>Hello World!</Say></Response>
response.
Once you see your sample PHP web application's <Response><Say>Hello World!</Say></Response>
message, your development environment is ready to go. But for most Twilio projects, you'll want to install one more helpful tool: ngrok.
Most Twilio services use webhooks to communicate with your application. When Twilio receives an incoming phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.
When you're working on your PHP web application in your development environment, your app is only reachable by other programs on the same computer, so Twilio won't be able to communicate with it.
ngrok is a tool for solving this problem. Once started, it provides a unique URL on the ngrok.io
domain that will forward incoming requests to your local development environment.
There are several ways to install ngrok on macOS:
Option 1: Using Homebrew (recommended)
brew install ngrok
Option 2: Download manually
- Download the binary from the ngrok download page for your operating system.
- Extract the downloaded file and move it to a directory in your PATH:
1sudo mv ngrok /usr/local/bin/ngrok2sudo chmod +x /usr/local/bin/ngrok
Option 3: Install globally with npm
npm install -g ngrok
Once ngrok has been installed, make sure your PHP web application is running on localhost port 3000, then start ngrok with the following command, perhaps in a second terminal:
ngrok http 3000
If ngrok is not in your PATH and you downloaded it manually, you may need to run:
./ngrok http 3000
Note: Use this command from the directory where you extracted the ngrok binary.
The output should look similar to this:

Find the "Forwarding" line in the output to see your unique ngrok domain name (for example, aaf29606.ngrok.io
). Open your web browser and enter this domain name in the address bar.
If everything is working correctly, you should see your PHP web application's <Response><Say>Hello World!</Say></Response>
message displayed at your new ngrok URL.
Anytime you're working on your Twilio application and need a URL for a webhook, you should use ngrok to get a publicly accessible URL like this one.
You're ready to build out your PHP web application. Learn more with the following resources: