如何在 Azure 上的 PHP 應用程式中使用 Twilio 撥打電話How to Make a Phone Call Using Twilio in a PHP Application on Azure

下列範例將說明如何從 Azure 代管的 PHP 網頁上使用 Twilio 撥打電話。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. 從您的 Twilio 主控台取得 Twilio 帳戶和驗證權杖。Acquire a Twilio account and authentication token from your Twilio Console. 若要開始使用 Twilio,請在 http://www.twilio.com/pricing 上評估價格。To get started with Twilio, evaluate pricing at http://www.twilio.com/pricing. 您可以在 https://www.twilio.com/try-twilio 上註冊試用帳戶。You can sign up for a trial account at https://www.twilio.com/try-twilio.
  2. 取得 適用於 PHP 的 Twilio 程式庫 ,或以 PEAR 封裝的形式進行安裝。Obtain the Twilio library for PHP or install it as a PEAR package. 如需詳細資訊,請參閱 讀我檔案For more information, see the readme file.
  3. 安裝 Azure SDK for PHP。Install the Azure SDK for PHP.

建立用以撥打電話的 Web 表單Create a web form for making a call

下列 HTML 程式碼將說明如何建置網頁 (callform.html),以擷取撥打電話所需的使用者資料:The following HTML code shows how to build a web page (callform.html) that retrieves user data for making a call:

<!DOCTYPE html>
<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 name="callTo" size="50" type="text" value=""></td>
      </tr>
      <tr>
        <td>From:</td>
        <td><input name="callFrom" size="50" type="text" value=""></td>
      </tr>
      <tr>
        <td>Call message:</td>
        <td><input name="callText" size="100" type="text" 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

下列程式碼將說明如何建置會在使用者提交 callform.html 所顯示的表單時受到呼叫的 makecall.phpThe following code shows how to build 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. 同時,請務必使用來自 Twilio 主控台的 Twilio 帳戶和驗證權杖,而不是下方程式碼中指派給 $sid$token 的預留位置值。Also, be sure to use your Twilio account and authentication token from the Twilio Console 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 'path/to/vendor/autoload.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 Twilio\Rest\Client($sid, $token);

$call = $client->calls->create(
            $to_number,
            $from_number,
            array('url' => http://twimlets.com/message?Message=' . urlencode($message))
        );

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

除了撥打電話以外,makecall.php 也會顯示某些通話中繼資料,如以下影像中所示。In addition to making the call, makecall.php displays some call metadata, as is shown in the image below. 如需通話中繼資料的詳細資訊,請參閱 https://www.twilio.com/docs/api/rest/call#instance-propertiesFor 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

下一個步驟是將應用程式部署至 Azure Web Apps 與 Git (其中的資訊並非全部與之相關)。The next step is to deploy your application to Azure Web Apps with Git (though not all the information there is relevant).

後續步驟Next steps

此程式可說明在 Azure 上的 PHP 中使用 Twilio 的基本功能。This code was provided to show you basic functionality using Twilio in PHP on Azure. 在部署至生產環境中的 Azure 之前,您可以新增更多錯誤處理或其他功能。Before deploying to Azure in production, you may want to add more error handling or other features. 例如:For example:

如需 Twilio 的其他資訊,請參閱 https://www.twilio.com/docsFor additional information about Twilio, see https://www.twilio.com/docs.

另請參閱See Also