How to Make a Phone Call Using Twilio in a PHP Application on Windows Azure
The following example shows you how you can use Twilio to make a call from a PHP web page hosted in Windows Azure. The resulting application will prompt the user for phone call values, as shown in the following screen shot.
You'll need to do the following to use the code in this topic:
- Acquire a Twilio account and authentication token. To get started with Twilio, evaluate pricing at http://www.twilio.com/pricing. You can sign up for a trial account at https://www.twilio.com/try-twilio. For information about the API provided by Twilio, see http://www.twilio.com/api.
- Verify your phone number as an outbound caller ID with Twilio. For information on how to verify your phone number, see https://www.twilio.com/user/account/phone-numbers/verified#. As an alternative to using an existing number, you can purchase a Twilio phone number.
For purposes of this example, use the verified phone number as the From value of callform.php (described later).
- Obtain the Twilio library for PHP. You can download it from Github (https://github.com/twilio/twilio-php) or install it as a PEAR package. For more information, see https://github.com/twilio/twilio-php/blob/master/README.md.
- Install the Windows Azure SDK for PHP. For an overview of the SDK and instructions on installing it, see Set up the Windows Azure SDK for PHP.
Create a web form for making a call
The following HTML code shows how to build a web page (callform.html) that retrieves user data for making a call:
<title>Automated call form</title>
<h1>Automated Call Form</h1>
<p>Fill in all fields and click <b>Make this call</b>.</p>
<form action="makecall.php" method="post">
<td><input type="text" size=50 name="callTo" value=""></td>
<td><input type="text" size=50 name="callFrom" value=""></td>
<td><input type="text" size=100 name="callText" value="Hello. This is the call text. Good bye." /></td>
<td colspan=2><input type="submit" value="Make this call"></td>
Create the code to make the call
The following code shows how to build a web page (makecall.php) which is called when the user submits the form displayed by callform.html. The code shown below creates the call message and generates the call. (Use your Twilio account and authentication token instead of the placeholder values assigned to $sid and $token in the code below.)
<p>Your call is being made.</p>
$sid = "your_account_sid";
$token = "your_authentication_token";
$from_number = $_POST['callFrom']; // Calls must be made from a registered Twilio number.
$to_number = $_POST['callTo'];
$message = $_POST['callText'];
$client = new Services_Twilio($sid, $token, "2010-04-01");
$call = $client->account->calls->create(
echo "Call status: ".$call->status."<br />";
echo "URI resource: ".$call->uri."<br />";
In addition to making the call, makecall.php displays some call metadata (example shown in screenshot below). For more information about call metadata, see https://www.twilio.com/docs/api/rest/call#instance-properties.
Run the application
The next step is to deploy your application to Windows Azure Web Sites. The following articles contain the information for creating a web site and deploying your code with Git, FTP, or WebMatrix (though not all information in each article is relevant):
This code was provided to show you basic functionality using Twilio in PHP on Windows Azure. Before deploying to Windows Azure in production, you may want to add more error handling or other features. For example:
For additional information about Twilio, see https://www.twilio.com/docs.