twilio / sdk
Twilio API的PHP包装器
Requires
- php: >=7.1.0
Requires (Dev)
- guzzlehttp/guzzle: ^6.3 || ^7.0
- phpunit/phpunit: >=7.0 < 10
Suggests
- guzzlehttp/guzzle: An HTTP client to execute the API requests
- dev-main
- 8.3.1
- 8.3.0
- 8.2.3
- 8.2.2
- 8.2.1
- 8.2.0
- 8.1.1
- 8.1.0
- 8.0.1
- 8.0.0
- 8.0.0-rc.1
- 7.16.2
- 7.16.1
- 7.16.0
- 7.15.0
- 7.14.0
- 7.13.1
- 7.13.0
- 7.12.3
- 7.12.2
- 7.12.1
- 7.12.0
- 7.11.1
- 7.11.0
- 7.10.0
- 7.9.0
- 7.8.0
- 7.7.1
- 7.7.0
- 7.6.0
- 7.5.0
- 7.4.2
- 7.4.1
- 7.4.0
- 7.3.0
- 7.2.0
- 7.1.0
- 7.0.0
- 7.0.0-rc.4
- 7.0.0-rc.3
- 7.0.0-rc.2
- 7.0.0-rc.1
- 6.44.4
- 6.44.3
- 6.44.2
- 6.44.1
- 6.44.0
- 6.43.4
- 6.43.3
- 6.43.2
- 6.43.1
- 6.43.0
- 6.42.2
- 6.42.1
- 6.42.0
- 6.41.0
- 6.40.1
- 6.40.0
- 6.39.0
- 6.38.0
- 6.37.3
- 6.37.2
- 6.37.1
- 6.37.0
- 6.36.1
- 6.36.0
- 6.35.1
- 6.35.0
- 6.34.0
- 6.33.1
- 6.33.0
- 6.32.0
- 6.31.2
- 6.31.1
- 6.31.0
- 6.30.0
- 6.29.0
- 6.28.3
- 6.28.2
- 6.28.1
- 6.28.0
- 6.27.1
- 6.27.0
- 6.26.0
- 6.25.0
- 6.24.1
- 6.24.0
- 6.23.0
- 6.22.0
- 6.21.0
- 6.20.0
- 6.19.0
- 6.18.0
- 6.17.0
- 6.16.1
- 6.16.0
- 6.15.1
- 6.15.0
- 6.14.0
- 6.13.0
- 6.12.0
- 6.11.0
- 6.10.4
- 6.10.3
- 6.10.2
- 6.10.1
- 6.10.0
- 6.9.2
- 6.9.1
- 6.9.0
- 6.8.0
- 6.7.0
- 6.6.0
- 6.5.0
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.1
- 6.0.0
- 5.42.2
- 5.42.1
- 5.42.0
- 5.41.1
- 5.41.0
- 5.40.0
- 5.39.0
- 5.38.0
- 5.37.0
- 5.36.1
- 5.36.0
- 5.35.0
- 5.34.4
- 5.34.3
- 5.34.2
- 5.34.1
- 5.34.0
- 5.33.0
- 5.32.1
- 5.32.0
- 5.31.3
- 5.31.2
- 5.31.1
- 5.31.0
- 5.30.2
- 5.30.1
- 5.30.0
- 5.29.1
- 5.29.0
- 5.28.1
- 5.28.0
- 5.27.1
- 5.27.0
- 5.26.0
- 5.25.0
- 5.24.2
- 5.24.1
- 5.24.0
- 5.23.1
- 5.23.0
- 5.22.0
- 5.21.4
- 5.21.3
- 5.21.2
- 5.21.1
- 5.21.0
- 5.20.0
- 5.19.7
- 5.19.6
- 5.19.5
- 5.19.4
- 5.19.3
- 5.19.2
- 5.19.1
- 5.19.0
- 5.18.0
- 5.17.1
- 5.17.0
- 5.16.7
- 5.16.6
- 5.16.5
- 5.16.4
- 5.16.3
- 5.16.2
- 5.16.1
- 5.16.0
- 5.15.6
- 5.15.5
- 5.15.4
- 5.15.3
- 5.15.2
- 5.15.1
- 5.15.0
- 5.14.1
- 5.14.0
- 5.13.4
- 5.13.3
- 5.13.2
- 5.13.1
- 5.13.0
- 5.12.1
- 5.12.1-alpha1
- 5.11.0
- 5.11.0-alpha1
- 5.10.0
- 5.10.0-alpha1
- 5.9.1
- 5.9.0
- 5.9.0-alpha1
- 5.8.0
- 5.8.0-alpha1
- 5.7.3
- 5.7.3-alpha1
- 5.7.2
- 5.7.2-alpha1
- 5.7.1
- 5.7.1-alpha2
- 5.7.1-alpha1
- 5.7.0
- 5.6.0
- 5.6.0-alpha1
- 5.5.0
- 5.5.0-alpha1
- 5.4.2
- 5.4.2-alpha1
- 5.4.1
- 5.4.1-alpha1
- 5.4.0
- 5.4.0-alpha1
- 5.3.0
- 5.3.0-alpha1
- 5.2.0
- 5.2.0-alpha1
- 5.1.1
- 5.1.1-alpha1
- 5.1.0
- 5.1.0-alpha2
- 5.1.0-alpha1
- 5.0.3
- 5.0.3-alpha1
- 5.0.2
- 5.0.2-alpha1
- 5.0.1
- 5.0.1-alpha1
- 5.0.0
- 5.0.0-RC22
- 5.0.0-RC21
- 5.0.0-RC20
- 5.0.0-RC19
- 5.0.0-RC18
- 5.0.0-RC17
- 5.0.0-RC16
- 5.0.0-RC15
- 5.0.0-RC14
- 5.0.0-RC13
- 5.0.0-RC12
- 5.0.0-RC11
- 5.0.0-RC10
- 5.0.0-RC9
- 5.0.0-RC8
- 5.0.0-RC7
- 5.0.0-RC6
- 5.0.0-RC5
- 5.0.0-RC4
- 5.0.0-RC3
- 5.0.0-RC2
- 5.0.0-RC1
- 4.12.1
- 4.12.0
- 4.11.0
- 4.10.0
- 4.9.3
- 4.9.2
- 4.9.1
- 4.9.0
- 4.8.1
- 4.8.0
- 4.7.0
- 4.6.1
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.13.1
- 3.13.0
- 3.12.8
- 3.12.7
- 3.12.6
- 3.12.5
- 3.12.4
- 3.12.3
- 3.12.2
- 3.12.1
- 3.12.0
- 3.10.0
- 3.9.1
- 3.9.0
- 3.8.3
- 3.8.2
- 3.8.1
- 3.8.0
- 3.7.2
- 3.7.1
- 3.7.0
- 3.6.0
- 3.5.2
- 3.5.1
- 3.5.0
- dev-8.0.0-rc
- dev-json-support
- dev-test-branch
- dev-upgrade-md-file
- dev-DI-2457
- dev-docstrings_remaining
- dev-remaining_docstrings
- dev-remaining_docstring
- dev-rc-to-main
- dev-7.0.0-rc
- dev-rc-cluster-test
- dev-sonarcloud
- dev-pathfix
- dev-legacy
This package is auto-updated.
Last update: 2024-09-18 13:42:13 UTC
README
文档
Twilio API的文档可以在这里找到。
PHP库的文档可以在这里找到。
版本
twilio-php
使用修改版的语义版本控制来处理所有变更。查看此文档以获取详细信息。
支持的PHP版本
此库支持以下PHP实现
- PHP 7.2
- PHP 7.3
- PHP 7.4
- PHP 8.0
- PHP 8.1
- PHP 8.2
- PHP 8.3
安装
您可以通过Composer或下载源代码来安装twilio-php
。
通过Composer
twilio-php
作为twilio/sdk
软件包在Packagist上提供
composer require twilio/sdk
测试您的安装
以下是一个使用SDK发送短信的示例
// Send an SMS using Twilio's REST API and PHP <?php // Required if your environment does not handle autoloading require __DIR__ . '/vendor/autoload.php'; // Your Account SID and Auth Token from console.twilio.com $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); // Use the Client to make requests to the Twilio REST API $client->messages->create( // The number you'd like to send the message to '+15558675309', [ // A Twilio phone number you purchased at https://console.twilio.com 'from' => '+15017250604', // The body of the text message you'd like to send 'body' => "Hey Jenny! Good luck on the bar exam!" ] );
不使用Composer
虽然我们建议使用包管理器跟踪您应用程序中的依赖项,但您仍然可以手动下载和使用PHP SDK。您可以从GitHub下载PHP SDK的完整源代码,并浏览仓库。要在您的应用程序中使用SDK,请将SDK下载文件解压缩到与您的PHP代码相同的目录中。然后,您可以在代码中包含SDK附带的自加载文件。
<?php // Require the bundled autoload file - the path may need to change // based on where you downloaded and unzipped the SDK require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php'; // Your Account SID and Auth Token from console.twilio.com $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); // Use the Client to make requests to the Twilio REST API $client->messages->create( // The number you'd like to send the message to '+15558675309', [ // A Twilio phone number you purchased at https://console.twilio.com 'from' => '+15017250604', // The body of the text message you'd like to send 'body' => "Hey Jenny! Good luck on the bar exam!" ] );
用法
拨打电话
<?php $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); // Read TwiML at this URL when a call connects (hold music) $call = $client->calls->create( '8881231234', // Call this number '9991231234', // From a valid Twilio number [ 'url' => 'https://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient' ] );
警告在本地测试时,您可以硬编码凭据,但在提交代码或部署到生产之前,您应该使用环境变量来保持它们保密。有关更多信息,请参阅如何设置环境变量。
获取现有的调用
<?php require_once '/path/to/vendor/autoload.php'; $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); // Get an object using its SID. If you do not have a SID, // check out the list resource examples on this page $call = $client->calls("CA42ed11f93dc08b952027ffbc406d0868")->fetch(); print $call->to;
遍历记录
库会自动为您处理分页。例如,calls
和messages
等集合具有在内部进行分页的read
和stream
方法。使用read
和stream
,您可以指定希望接收的记录数(limit
)以及每页检索的最大大小(pageSize
)。然后,库将为您处理这项任务。
read
会立即检索所有记录并将它们作为列表返回,而stream
会返回一个迭代器,并在您遍历集合时懒加载记录页。您还可以使用page
方法手动分页。
<?php require_once '/path/to/vendor/autoload.php'; $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); $limit = 5; $pageSize = 2; // Read - fetches all messages eagerly and returns as a list $messageList = $client->messages->read([], $limit); foreach ($messageList as $msg) { print($msg->sid); } // Stream - returns an iterator of 'pageSize' messages at a time and lazily retrieves pages until 'limit' messages $messageStream = $client->messages->stream([], $limit, $pageSize); foreach ($messageStream as $msg) { print($msg->sid); } // Page - get the a single page by passing pageSize, pageToken and pageNumber $messagePage = $client->messages->page([], $pageSize); $nextPageData = $messagePage->nextPage(); // this will return data of next page foreach ($messagePage as $msg) { print($msg->sid); }
有关这些方法的更多信息,请查看自动生成的库文档。
使用read
方法
<?php require_once '/path/to/vendor/autoload.php'; $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); // Loop over the list of calls and print a property from each one foreach ($client->calls->read() as $call) { print $call->direction; }
指定区域和/或边缘
要利用Twilio的全球基础设施,请指定客户端的目标区域和/或边缘
<?php $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token, null, 'au1'); $client->setEdge('sydney');
没有这些参数的Client
构造函数也将查找当前环境中的TWILIO_REGION
和TWILIO_EDGE
变量。
这将导致hostname
从api.twilio.com
变为api.sydney.au1.twilio.com
。
启用调试日志记录
在默认的HTTP客户端中,有两种方法可以启用调试日志。您可以创建一个名为 TWILIO_LOG_LEVEL
的环境变量,并将其设置为 debug
,或者您可以将日志级别设置为调试。
$sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); $client->setLogLevel('debug');
生成TwiML
为了控制电话,您的应用程序需要输出 TwiML。
使用 Twilio\TwiML\(Voice|Messaging|Fax)Response
轻松串联这些响应。
<?php $response = new Twilio\TwiML\VoiceResponse(); $response->say('Hello'); $response->play('https://api.twilio.com/cowbell.mp3', ['loop' => 5]); print $response;
这将输出如下XML:
<?xml version="1.0" encoding="utf-8"?> <Response> <Say>Hello</Say> <Play loop="5">https://api.twilio.com/cowbell.mp3</Play> </Response>
处理异常
当客户端初始化、API请求或创建TwiML时出现问题时,twilio-php 将抛出适当的异常。您应该处理这些异常以保持应用程序运行,避免不必要的崩溃。
Twilio客户端
例如,当初始化客户端时,可能会遇到认证异常,可能是由于凭证错误。这可以这样处理:
<?php require_once('/path/to/twilio-php/Services/Twilio.php'); use Twilio\Exceptions\ConfigurationException; use Twilio\Rest\Client; $sid = "ACXXXXXX"; $token = "YYYYYY"; // Attempt to create a new Client, but your credentials may contain a typo try { $client = new Twilio\Rest\Client($sid, $token); } catch (ConfigurationException $e) { // You can `catch` the exception, and perform any recovery method of your choice print $e->getCode(); } $call = $client->account->calls ->get("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); print $call->to;
CurlClient
在初始化curl客户端时,如果您系统上未安装curl,您将看到 EnvironmentException。
<?php require_once('/path/to/twilio-php/Services/Twilio.php'); use Twilio\Exceptions\TwilioException; use Twilio\Http\CurlClient; $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); try { $client = new CurlClient(); $client->options( 'GET', 'http://api.twilio.com', array(), array(), array(), $sid, $token ); } catch (EnvironmentException $e) { print $e->getCode(); } print $call->to;
TwilioException
TwilioException
可以用来处理API错误,如下所示。这是您最有可能使用的最常见异常类型。
<?php require_once('/path/to/twilio-php/Services/Twilio.php'); use Twilio\Exceptions\TwilioException; $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); try { $call = $client->account->calls ->get("CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); } catch (TwilioException $e) { print $e->getCode(); } print $call->to;
TwimlException
当使用 twilio-php
构建 TwiML 时,如果结果不符合API的预期,您将看到 TwimlException
,您需要像这样处理它:
<?php require_once './vendor/autoload.php'; use Twilio\Twiml; try { $response = new Twiml(); $dial = $response->dial(); $dial->conference('Room 1234'); print $response; } catch (TwimlException $e) { print $e->getCode(); }
调试API请求
为了辅助调试,库允许您访问底层请求和响应对象。这种能力集成在库中提供的默认Curl客户端中。
例如,您可以像这样检索最后响应的状态码:
<?php $sid = "ACXXXXXX"; $token = "YYYYYY"; $client = new Twilio\Rest\Client($sid, $token); $message = $client->messages->create( '+15558675309', [ 'from' => '+15017250604', 'body' => "Hey Jenny! Good luck on the bar exam!" ] ); // Print the message's SID print $message->sid; // Print details about the last request print $client->lastRequest->method; print $client->lastRequest->url; print $client->lastRequest->auth; print $client->lastRequest->params; print $client->lastRequest->headers; print $client->lastRequest->data; // Print details about the last response print $client->lastResponse->statusCode; print $client->lastResponse->body;
使用自定义HTTP客户端
要使用此辅助库与自定义HTTP客户端一起使用,请参阅 如何操作的进阶示例。
Docker镜像
此存储库中现有的 Dockerfile
和相应的 twilio/twilio-php
Docker镜像目前仅由Twilio用于测试目的。
获取帮助
如果您需要安装或使用库的帮助,请首先检查 Twilio支持帮助中心,如果您找不到问题的答案,请 提交支持工单。
如果您在库中发现了错误或希望添加新功能,请继续在此存储库中提交问题或拉取请求!