In this guide we'll cover how to set up your PHP development environment. We’ll also talk about a couple helpful tools that we recommend for all PHP applications that use Twilio: ngrok and the Twilio PHP SDK.
Let’s get started!
How you install PHP varies depending on your operating system.
|OS X||The easiest way to install PHP on OS X is to use the official installer from php.net. You can also use Homebrew if you prefer.
|Windows||To install PHP on Windows is using the official installer from php.net. You can also use Chocolatey if you prefer.|
|Linux||The exact instructions to install PHP vary by distribution. Find instructions for Ubuntu or Debian.|
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.
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.
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.
$ mkdir twilio-php-app $ cd twilio-php-app $ composer init --require=twilio/sdk
We’re almost ready to start writing our PHP application, but first we need to install the Twilio PHP SDK.
# Use composer to install the Twilio PHP SDK. $ composer install Loading composer repositories with package information Updating dependencies (including require-dev) - Installing twilio/sdk (5.4.2) Loading from cache Writing lock file Generating autoload files
We can test that our development environment is configured correctly by creating a simple PHP application.
<?php require_once "vendor/autoload.php"; use Twilio\Twiml; $response = new Twiml; $response->say("Hello World!"); header("content-type: text/xml"); 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 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.
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 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
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:
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.