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.
Let's get started!
How you install PHP varies depending on your operating system.
|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.
$ brew install php
|To install PHP on Windows use the official installer from php.net. You can also use Chocolatey if you prefer.
|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.
_10mkdir twilio-php-app_10cd twilio-php-app_10_10composer init --require=twilio/sdk
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_10Loading composer repositories with package information_10Updating dependencies (including require-dev)_10 - Installing twilio/sdk (5.4.2)_10 Loading from cache_10_10Writing lock file_10Generating autoload files
We can test that our development environment is configured correctly by creating a simple PHP application.
_10<?php_10require_once "vendor/autoload.php";_10_10use Twilio\TwiML\VoiceResponse;_10_10$response = new VoiceResponse;_10$response->say("Hello World!");_10_10header("content-type: text/xml");_10echo $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.
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.
You're now ready to build out your PHP web application. Here are a few other resources we like: