您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.
如何通过 PHP 使用 Twilio 实现语音和 SMS 功能How to Use Twilio for Voice and SMS Capabilities in PHP
11/25/2014
本文内容
本指南演示如何在 Azure 中使用 Twilio API 服务执行常见编程任务。This guide demonstrates how to perform common programming tasks with the Twilio API service on Azure. 所涉及的任务包括发起电话呼叫和发送短信服务 (SMS) 消息。The scenarios covered include making a phone call and sending a Short Message Service (SMS) message. 有关 Twilio 以及在应用程序中使用语音和短信的详细信息,请参阅 后续步骤 部分。For more information on Twilio and using voice and SMS in your applications, see the Next Steps section.
什么是 Twilio?What is Twilio?
Twilio 为将来的商业沟通提供强大支持,并使开发人员能够将语音、VoIP 和消息传送嵌入到应用程序中。Twilio is powering the future of business communications, enabling developers to embed voice, VoIP, and messaging into applications. 它们对基于云的全球环境中所需的所有基础结构进行虚拟化,并通过 Twilio 通信 API 平台将其公开。They virtualize all infrastructure needed in a cloud-based, global environment, exposing it through the Twilio communications API platform. 可轻松构建和扩展应用程序。Applications are simple to build and scalable. 享受现用现付定价所带来的灵活性,并从云可靠性中受益。Enjoy flexibility with pay-as-you go pricing, and benefit from cloud reliability.
利用 Twilio 语音,应用程序可以发起和接收电话呼叫。Twilio Voice allows your applications to make and receive phone calls. Twilio SMS 使应用程序能够发送和接收文本消息。Twilio SMS enables your application to send and receive text messages. 利用 Twilio 客户端,可以从任何手机、平板电脑或浏览器发起 VoIP 呼叫并支持 WebRTC。Twilio Client allows you to make VoIP calls from any phone, tablet, or browser and supports WebRTC.
Twilio 定价和特惠套餐Twilio Pricing and Special Offers
Azure 客户在升级 Twilio 帐户后即可获得特惠套餐:10 美元的 Twilio 信用额度。Azure customers receive a special offer: complimentary $10 of Twilio Credit when you upgrade your Twilio Account. 此 Twilio 信用可应用于任何 Twilio 使用(10 美元信用等价于发送多达 1,000 条 SMS 消息或接收长达 1000 分钟的入站语音,具体取决电话号码和消息或呼叫目标的位置)。This Twilio Credit can be applied to any Twilio usage ($10 credit equivalent to sending as many as 1,000 SMS messages or receiving up to 1000 inbound Voice minutes, depending on the location of your phone number and message or call destination). Redeem this Twilio credit and get started at: https://ahoy.twilio.com/azure.
Twilio 是一种现用现付服务。Twilio is a pay-as-you-go service. 没有设置费用,并且可以随时关闭帐户。There are no set-up fees and you can close your account at any time. 可以在 Twilio 定价中找到更多详细信息。You can find more details at Twilio Pricing.
概念Concepts
Twilio API 是一个为应用程序提供语音和 SMS 功能的 RESTful API。The Twilio API is a RESTful API that provides voice and SMS functionality for applications. 提供了多种语言版本的客户端库;有关列表,请参阅 Twilio API 库。Client libraries are available in multiple languages; for a list, see Twilio API Libraries.
Twilio API 的关键方面是 Twilio 谓词和 Twilio 标记语言 (TwiML)。Key aspects of the Twilio API are Twilio verbs and Twilio Markup Language (TwiML).
Twilio 谓词Twilio Verbs
API 利用了 Twilio 谓词;例如, < 口述 > 谓词指示 Twilio 在调用时呼叫时传递一条消息。The API makes use of Twilio verbs; for example, the verb instructs Twilio to audibly deliver a message on a call.
下面是 Twilio 谓词的列表。The following is a list of Twilio verbs. Learn about the other verbs and capabilities via Twilio Markup Language documentation.
< 拨 >:将呼叫方连接到其他电话。: Connects the caller to another phone.
< 收集 >:收集在电话键盘上输入的数字。: Collects numeric digits entered on the telephone keypad.
< 挂断 >:结束呼叫。: Ends a call.
< Play >:播放音频文件。: Plays an audio file.
< 暂停 >:在指定的秒数后自动等待。: Waits silently for a specified number of seconds.
< 记录 >:记录调用方的声音并返回包含该记录的文件的 URL。: Records the caller's voice and returns a URL of a file that contains the recording.
< 重 > 定向:将对呼叫或 SMS 的控制转移到不同 URL 处的 TwiML。: Transfers control of a call or SMS to the TwiML at a different URL.
< 拒绝 >:拒绝对 Twilio 号码的传入呼叫而不向你计费: Rejects an incoming call to your Twilio number without billing you
< 例如 >:将文本转换为在调用时发出的语音。: Converts text to speech that is made on a call.
< Sms >:发送短信。: Sends an SMS message.
TwiMLTwiML
TwiML 是一组基于 XML 的指令,这些指令以用于指示 Twilio 如何处理呼叫或 SMS 的 Twilio 谓词为基础。TwiML is a set of XML-based instructions based on the Twilio verbs that inform Twilio of how to process a call or SMS.
例如,以下 TwiML 将文本 Hello World 转换为语音。As an example, the following TwiML would convert the text Hello World to speech.
Hello World
当应用程序调用 Twilio API 时,某个 API 参数将为返回 TwiML 响应的 URL。When your application calls the Twilio API, one of the API parameters is the URL that returns the TwiML response. 在开发过程中,可以使用 Twilio 提供的 URL 来提供应用程序所使用的 TwiML 响应。For development purposes, you can use Twilio-provided URLs to provide the TwiML responses used by your applications. 还可以托管自己的 URL 来生成 TwiML 响应,也可以选择使用 TwiMLResponse 对象。You could also host your own URLs to produce the TwiML responses, and another option is to use the TwiMLResponse object.
有关 Twilio 谓词、其属性和 TwiML 的详细信息,请参阅 TwiML。For more information about Twilio verbs, their attributes, and TwiML, see TwiML. 有关 Twilio API 的其他信息,请参阅 Twilio API。For additional information about the Twilio API, see Twilio API.
创建 Twilio 帐户Create a Twilio Account
准备好获取 Twilio 帐户后,请在试用 Twilio 上注册。When you're ready to get a Twilio account, sign up at Try Twilio. 可以先使用免费帐户,以后再升级帐户。You can start with a free account, and upgrade your account later.
注册 Twilio 帐户时,将收到帐户 ID 和身份验证令牌。When you sign up for a Twilio account, you'll receive an account ID and an authentication token. 需要二者才能发起 Twilio API 呼叫。Both will be needed to make Twilio API calls. 为了防止对帐户进行未经授权的访问,请保护身份验证令牌。To prevent unauthorized access to your account, keep your authentication token secure. 帐户 ID 和身份验证令牌会分别显示在 Twilio 帐户页上标记为“帐户 SID”和“身份验证令牌”的字段中。Your account ID and authentication token are viewable at the Twilio account page, in the fields labeled ACCOUNT SID and AUTH TOKEN, respectively.
创建 PHP 应用程序Create a PHP Application
使用 Twilio 服务且在 Azure 中运行的 PHP 应用程序与任何其他使用 Twilio 服务的 PHP 应用程序之间没有任何差别。A PHP application that uses the Twilio service and is running in Azure is no different than any other PHP application that uses the Twilio service. Twilio 服务是基于 REST 的且可通过几种方法从 PHP 中调用,本文将重点介绍如何将 Twilio 服务与 GitHub 提供的用于 PHP 的 Twilio 库一起使用。While Twilio services are REST-based and can be called from PHP in several ways, this article will focus on how to use Twilio services with Twilio library for PHP from GitHub. For more information about using the Twilio library for PHP, see https://www.twilio.com/docs/libraries/php.
有关如何构建 Twilio/PHP 应用程序并将其部署到 Azure 的详细说明,请参阅如何在 Azure 中通过 PHP 应用程序使用 Twilio 发起电话呼叫。Detailed instructions for building and deploying a Twilio/PHP application to Azure are available at How to Make a Phone Call Using Twilio in a PHP Application on Azure.
将应用程序配置为使用 Twilio 库Configure Your Application to Use Twilio Libraries
可以通过两种方式将应用程序配置为使用用于 PHP 的 Twilio 库:You can configure your application to use the Twilio library for PHP in two ways:
从 GitHub 下载用于 PHP 的 Twilio 库 (https://github.com/twilio/twilio-php) 并将 " 服务 " 目录添加到应用程序。Download the Twilio library for PHP from GitHub (https://github.com/twilio/twilio-php) and add the Services directory to your application.
- 或 --OR-
将用于 PHP 的 Twilio 库作为 PEAR 包安装。Install the Twilio library for PHP as a PEAR package. 可使用以下命令安装它:It can be installed with the following commands:
$ pear channel-discover twilio.github.com/pear
$ pear install twilio/Services_Twilio
安装用于 PHP 的 Twilio 库后,可以在 PHP 文件的顶部添加 require_once 语句来引用该库:Once you have installed the Twilio library for PHP, you can then add a require_once statement at the top of your PHP files to reference the library:
require_once 'Services/Twilio.php';
如何拨打传出呼叫How to: Make an outgoing call
下面演示了如何使用 Services_Twilio 类发起传出呼叫。The following shows how to make an outgoing call using the Services_Twilio class. 此代码还使用 Twilio 提供的网站返回 Twilio 标记语言 (TwiML) 响应。This code also uses a Twilio-provided site to return the Twilio Markup Language (TwiML) response. 用自己的值替换“呼叫方”和“被呼叫方”电话号码,并确保在运行代码之前验证 Twilio 帐户的“呼叫方”电话号码。Substitute your values for the From and To phone numbers, and ensure that you verify the From phone number for your Twilio account prior to running the code.
// Include the Twilio PHP library.
require_once 'Services/Twilio.php';
// Library version.
$version = "2010-04-01";
// Set your account ID and authentication token.
$sid = "your_twilio_account_sid";
$token = "your_twilio_authentication_token";
// The number of the phone initiating the call.
$from_number = "NNNNNNNNNNN";
// The number of the phone receiving call.
$to_number = "NNNNNNNNNNN";
// Use the Twilio-provided site for the TwiML response.
$url = "https://twimlets.com/message";
// The phone message text.
$message = "Hello world.";
// Create the call client.
$client = new Services_Twilio($sid, $token, $version);
//Make the call.
try
{
$call = $client->account->calls->create(
$from_number,
$to_number,
$url.'?Message='.urlencode($message)
);
}
catch (Exception $e)
{
echo 'Error: ' . $e->getMessage();
}
如前所述,此代码使用 Twilio 提供的网站返回 TwiML 响应。As mentioned, this code uses a Twilio-provided site to return the TwiML response. 可以改用自己的网站来提供 TwiML 响应;有关详细信息,请参阅如何从自己的网站提供 TwiML 响应。You could instead use your own site to provide the TwiML response; for more information, see How to Provide TwiML Responses from Your Own Web Site.
Note: To troubleshoot TLS/SSL certificate validation errors, see https://www.twilio.com/docs/api/errors
如何发送短信How to: Send an SMS message
下面演示如何使用 Services_Twilio 类发送 SMS 消息。The following shows how to send an SMS message using the Services_Twilio class. " 发件 人" 号码由 Twilio 提供,供试用帐户用来发送短信。The From number is provided by Twilio for trial accounts to send SMS messages. 在运行代码前,必须验证 Twilio 帐户的 " 到 " 编号。The To number must be verified for your Twilio account prior to running the code.
// Include the Twilio PHP library.
require_once 'Services/Twilio.php';
// Library version.
$version = "2010-04-01";
// Set your account ID and authentication token.
$sid = "your_twilio_account_sid";
$token = "your_twilio_authentication_token";
$from_number = "NNNNNNNNNNN"; // With trial account, texts can only be sent from your Twilio number.
$to_number = "NNNNNNNNNNN";
$message = "Hello world.";
// Create the call client.
$client = new Services_Twilio($sid, $token, $version);
// Send the SMS message.
try
{
$client->$client->account->messages->sendMessage($from_number, $to_number, $message);
}
catch (Exception $e)
{
echo 'Error: ' . $e->getMessage();
}
如何从自己的网站提供 TwiML 响应How to: Provide TwiML Responses from your own Website
当应用程序发起对 Twilio API 的调用时,Twilio 会将请求发送到应返回 TwiML 响应的 URL。When your application initiates a call to the Twilio API, Twilio will send your request to a URL that is expected to return a TwiML response. The example above uses the Twilio-provided URL https://twimlets.com/message. (虽然 TwiML 专供 Twilio 使用,但可以在浏览器中查看它。(While TwiML is designed for use by Twilio, you can view the it in your browser. For example, click https://twimlets.com/message to see an empty element; as another example, click https://twimlets.com/message?Message%5B0%5D=Hello%20World to see a element that contains a element.)
可以创建自己的返回 HTTP 响应的网站,而不用依赖 Twilio 提供的 URL。Instead of relying on the Twilio-provided URL, you can create your own site that returns HTTP responses. 可以使用任何语言创建返回 XML 响应的站点;本主题假设要使用 PHP 创建 TwiML。You can create the site in any language that returns XML responses; this topic assumes you'll be using PHP to create the TwiML.
以下 PHP 页面将生成在呼叫时念出 Hello World 的 TwiML 响应。The following PHP page results in a TwiML response that says Hello World on the call.
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
?>
Hello world.
如上面的示例中所示,TwiML 响应只是一个 XML 文档。As you can see from the example above, the TwiML response is simply an XML document. 用于 PHP 的 Twilio 库包含将生成 TwiML 的类。The Twilio library for PHP contains classes that will generate TwiML for you. 下面的示例将生成与上面所示相同的响应,但该响应会使用用于 PHP 的 Twilio 库中的 Services_Twilio_Twiml 类:The example below produces the equivalent response as shown above, but uses the Services_Twilio_Twiml class in the Twilio library for PHP:
require_once('Services/Twilio.php');
$response = new Services_Twilio_Twiml();
$response->say("Hello world.");
print $response;
将 PHP 页面设置为提供 TwiML 响应后,请使用 PHP 页面的 URL 作为传入到 Services_Twilio->account->calls->create 方法中的 URL。Once you have your PHP page set up to provide TwiML responses, use the URL of the PHP page as the URL passed into the Services_Twilio->account->calls->create method. 例如,如果已将名为 MyTwiML 的 Web 应用程序部署到 Azure 托管服务,且 PHP 页面的名称将为 mytwiml.php,则可将 URL 传递到 Services_Twilio->account->calls->create,如以下示例所示:For example, if you have a Web application named MyTwiML deployed to an Azure hosted service, and the name of the PHP page is mytwiml.php, the URL can be passed to Services_Twilio->account->calls->create as shown in the following example:
require_once 'Services/Twilio.php';
$sid = "your_twilio_account_sid";
$token = "your_twilio_authentication_token";
$from_number = "NNNNNNNNNNN";
$to_number = "NNNNNNNNNNN";
$url = "http://.cloudapp.net/MyTwiML/mytwiml.php";
// The phone message text.
$message = "Hello world.";
$client = new Services_Twilio($sid, $token, "2010-04-01");
try
{
$call = $client->account->calls->create(
$from_number,
$to_number,
$url.'?Message='.urlencode($message)
);
}
catch (Exception $e)
{
echo 'Error: ' . $e->getMessage();
}
有关通过 PHP 在 Azure 中使用 Twilio 的其他信息,请参阅如何在 Azure 中通过 PHP 应用程序使用 Twilio 发起电话呼叫。For additional information about using Twilio in Azure with PHP, see How to Make a Phone Call Using Twilio in a PHP Application on Azure.
如何使用其他 Twilio 服务How to: Use Additional Twilio Services
除了此处所示的示例之外,Twilio 还提供了基于 Web 的 API,可通过这些 API 从 Azure 应用程序中使用其他 Twilio 功能。In addition to the examples shown here, Twilio offers web-based APIs that you can use to leverage additional Twilio functionality from your Azure application. 有关完整详细信息,请参阅 Twilio API 文档。For full details, see the Twilio API documentation.
后续步骤Next Steps
现在,已了解 Twilio 服务的基础知识,单击下面的链接可以了解详细信息:Now that you've learned the basics of the Twilio service, follow these links to learn more: