toucantext/php-sdk

此软件包的最新版本(1.1)没有可用的许可信息。

ToucanText PHP SDK 是一个简单易用的接口,可以帮助您快速高效地发送和检索消息。

1.1 2019-02-15 11:47 UTC

This package is auto-updated.

Last update: 2024-09-21 20:48:55 UTC


README

ToucanText

ToucanText PHP SDK

此库需要至少 PHP 版本 5.5

这是使用 ToucanText API 的 PHP SDK 库。要使用它,您需要一个 ToucanText 账户并有权访问您的 API 凭证。在 toucantext.com 上注册免费试用

安装

要使用 PHP SDK,请 创建 ToucanText 账户

要将 PHP 客户端安装到您的项目中,我们建议使用 Composer

composer requiure toucantext/php-sdk

您不需要克隆此存储库即可在您自己的项目中使用库,您可以使用 Composer 从 Packagist 安装它。

如果您是 Composer 新手,这里有一些您可能认为有用的资源

实例化 SDK 客户端

将配置传递给客户端

$config = [
  'username' => '{your_api_username}',
  'password' => '{your_api_password}'
];

$toucan = new ToucanText\Client($config);

注意:如果您不确定您的 'api_username' 或 'api_password' 是什么,请联系 info@toucantext.com

使用客户端

获取所有消息

返回您的所有消息列表(包括入站消息和投递回执)

// return all your messages
$toucan->messages->all();

默认情况下,这返回最多 25 条消息,但不会确认它们。要覆盖此限制,请传递以下参数(第一个参数表示是否确认消息;第二个参数表示返回的最大消息数)

// return 15 messages maximum and acknowledge them
$toucan->messages->all(true, 15);

获取入站消息或投递回执

返回入站消息或投递回执的列表

// return only inbound messages
$toucan->messages->get('messagesOnly');

// return only delivery receipts
$toucan->messages->get('dlrsOnly');

默认情况下,这返回最多 25 条入站消息或投递回执,但不会确认它们。要覆盖此限制,请传递以下参数(第二个参数表示是否确认消息;第三个参数表示返回的最大消息数)

// return 15 inbound messages maximum and acknowledge them
$toucan->messages->get('messagesOnly', true, 15);

// return 15 delivery receipts maximum and acknowledge them
$toucan->messages->get('dlrsOnly', true, 15);

发送消息

要发送消息,您可以调用以下操作

// send a message
$message = [
  'destinationAddress' => '{the_destination_address}',
  'message => '{your_message}'
];

$toucan->messages->send($message);

您还可以设置源地址并请求投递回执

// send a message with source address and request delivery receipt
$message = [
  'sourceAddress' => '{your_source_address}',
  'destinationAddress' => '{the_destination_address}',
  'message' => '{your_message}',
  'deliveryReceipt' => true
];

$toucan->messages->send($message);

确认投递回执和消息

当您检索您的入站消息或投递回执时,查询响应中有一个 MbStorageId 元素。此 ID 可以用于单独确认消息和投递回执。

要确认消息或投递回执,请创建一个包含要确认的 ID 的数组,然后调用以下操作

// array of message ID's to acknowledge
$messages = [
    245, 4564, 456
];

$toucan->messages->acknowledge($messages);

处理异常

除了可能由于调用而发生的错误之外,还可能会抛出其他异常。要处理它们,请将您的调用包裹在 try-catch 块中

try {
  $toucan->messages->all();
} catch (Exception $e) {
  // do something with $e
}

许可

此库根据 MIT 许可证 发布。