Skip to contentSkip to navigationSkip to topbar
On this page

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.


Prerequisites

prerequisites page anchor

Before you begin this setup guide, make sure you have:

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 systemInstructions
macOSThe easiest way to install PHP on macOS is to use the official installer from php.net(link takes you to an external page). You can also use Homebrew(link takes you to an external page) if you prefer: brew install php.
WindowsTo install PHP on Windows, use the official installer from php.net(link takes you to an external page). You can also use Chocolatey(link takes you to an external page) if you prefer: choco install php.
LinuxThe exact instructions to install PHP vary by distribution. Find instructions for Ubuntu or Debian(link takes you to an external page).

Install a text editor or IDE

install-a-text-editor-or-ide page anchor

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:


Composer(link takes you to an external page) 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 systemInstructions
macOSRecommended: Download and install from getcomposer.org(link takes you to an external page) using the installer script: curl -sS https://getcomposer.org/installer \\| php -- --install-dir=/usr/local/bin --filename=composer. Alternative: Use Homebrew(link takes you to an external page) if you have it working: brew install composer.
WindowsDownload the installer from getcomposer.org(link takes you to an external page) and run the setup wizard.
LinuxDownload and install using the command line instructions at getcomposer.org(link takes you to an external page), 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.

Troubleshooting Composer installation on macOS

troubleshooting-composer-installation-on-macos page anchor

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.

  1. Download and run the Composer installer:
curl -sS https://getcomposer.org/installer | php
  1. Move Composer to a directory in your PATH:
sudo mv composer.phar /usr/local/bin/composer
  1. Make it executable:
sudo chmod +x /usr/local/bin/composer
  1. Verify the installation:
composer --version

Start a new project with Composer

start-a-new-project-with-composer page anchor

To start a new project with Composer:

  1. Create a new empty directory in your development environment:
1
mkdir twilio-php-app
2
cd twilio-php-app
  1. 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.


Install the Twilio PHP SDK

install-the-twilio-php-sdk page anchor

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

Create a basic PHP application

create-a-basic-php-application page anchor

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
<?php
2
require_once "vendor/autoload.php";
3
4
use Twilio\TwiML\VoiceResponse;
5
6
$response = new VoiceResponse();
7
$response->say("Hello World!");
8
9
header("Content-Type: text/xml");
10
echo $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.


Set up ngrok for webhook development

set-up-ngrok-for-webhook-development page anchor

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(link takes you to an external page).

Most Twilio services use webhooks(link takes you to an external page) 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

  1. Download the binary from the ngrok download page(link takes you to an external page) for your operating system.
  2. Extract the downloaded file and move it to a directory in your PATH:
1
sudo mv ngrok /usr/local/bin/ngrok
2
sudo 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:

ngrok running, displaying the forwarding URL.

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: