Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
141 lines (114 sloc) 7.37 KB

How to Make a Phone Call Using Twilio in a PHP Application on Azure

The following example shows you how you can use Twilio to make a call from a PHP web page hosted in Azure. The resulting application will prompt the user for phone call values, as shown in the following screen shot.

Azure Call Form Using Twilio and PHP

You'll need to do the following to use the code in this topic:

  1. 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.
  2. Obtain the Twilio library for PHP or install it as a PEAR package. For more information, see the readme file.
  3. Install the Azure SDK for PHP. For an overview of the SDK and instructions on installing it, see Set up the 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:

<html>
<head>
    <title>Automated call form</title>
</head>
<body>
<h1>Automated Call Form</h1>
<p>Fill in all fields and click <b>Make this call</b>.</p>
<form action="makecall.php" method="post">
<table>
    <tr>
        <td>To:</td>
        <td><input type="text" size=50 name="callTo" value=""></td>
    </tr>
    <tr>
        <td>From:</td>
        <td><input type="text" size=50 name="callFrom" value=""></td>
    </tr>
    <tr>
        <td>Call message:</td>
        <td><input type="text" size=100 name="callText" value="Hello. This is the call text. Good bye." /></td>
    </tr>
    <tr>
        <td colspan=2><input type="submit" value="Make this call"></td>
    </tr>
</table>
</form>
<br/>
</body>
</html>

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.)

<html>
<head><title>Making call...</title></head>
<body>
<p>Your call is being made.</p>

<?php
require_once 'Services/Twilio.php';

$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(
    $from_number,
    $to_number,
    'http://twimlets.com/message?Message='.urlencode($message)
);

echo "Call status: ".$call->status."<br />";
echo "URI resource: ".$call->uri."<br />";
?>
</body>
</html>

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.

Azure Call Response Using Twilio and PHP

Run the application

The next step is to deploy your application to Azure Websites. The following articles contain the information for creating a website and deploying your code with Git, FTP, or WebMatrix (though not all information in each article is relevant):

Next steps

This code was provided to show you basic functionality using Twilio in PHP on Azure. Before deploying to 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.

See Also