Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

How to set up your PHP development environment


In this guide, we'll cover how to set up your PHP development environment. We'll also talk about a couple of helpful tools that we recommend for all PHP applications that use Twilio: ngrok and the Twilio PHP SDK(link takes you to an external page).

Let's get started!


Install PHP

install-php page anchor

How you install PHP varies depending on your operating system.

Operating SystemInstructions
OS XThe easiest way to install PHP on OS X 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.
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 we can start our PHP project we'll need something to write it with.

If you already have a code-writing tool of choice, you can stick with it for developing your PHP application. If you're looking for something new, we recommend trying out a few options:

If you're new to programming, we recommend giving Atom and Sublime Text a try before you settle on your favorite.


Start a new project with Composer

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

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.

Create a new empty directory in your development environment and run composer init. Composer will create a new composer.json file for you when you're done.


_10
mkdir twilio-php-app
_10
cd twilio-php-app
_10
_10
composer init --require=twilio/sdk


Install the Twilio PHP SDK

install-the-twilio-php-sdk page anchor

We're almost ready to start writing our PHP application, but first, we need to install the Twilio PHP SDK.


_10
# Use composer to install the Twilio PHP SDK.
_10
$ composer install
_10
Loading composer repositories with package information
_10
Updating dependencies (including require-dev)
_10
- Installing twilio/sdk (5.4.2)
_10
Loading from cache
_10
_10
Writing lock file
_10
Generating autoload files


Create a simple PHP application

create-a-simple-php-application page anchor

We can test that our development environment is configured correctly by creating a simple PHP application.


_10
<?php
_10
require_once "vendor/autoload.php";
_10
_10
use Twilio\TwiML\VoiceResponse;
_10
_10
$response = new VoiceResponse;
_10
$response->say("Hello World!");
_10
_10
header("content-type: text/xml");
_10
echo $response;

We can then try running our new PHP web application with the command php -S localhost:3000. You can then open http://localhost:3000 in your browser and after navigating to http://localhost:3000/file_name.php you should 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(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 talk to it.

Ngrok is our favorite tool for solving this problem. Once started, it provides a unique URL on the ngrok.io domain which will forward incoming requests to your local development environment.

To start, head over to the Ngrok download page and grab the binary for your operating system: https://ngrok.com/download(link takes you to an external page)

Once downloaded, make sure your PHP web application is running and then start Ngrok using this command: ./ngrok http 3000. You should see output similar to this:

ngrok screen.

Look at the "Forwarding" line to see your unique Ngrok domain name (ours is aaf29606.ngrok.io) and then point your browser at that domain name.

If everything's 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 now ready to build out your PHP web application. Here are a few other resources we like:

Twilio

twilio page anchor

Rate this page: