webhonar/sms-client

一个通用的短信客户端库。支持多个可替换的驱动程序。

1.0.14 2020-01-30 19:47 UTC

This package is not auto-updated.

Last update: 2024-09-28 11:06:42 UTC


README

Build Status

一个通用的短信客户端库。支持多个可替换的驱动程序,因此您不必仅限于使用一个提供商。

这个库专门用于发送短信,我不打算添加其他功能的支持。想法是创建一个库,它应该能够与任何具有发送短信目的驱动程序的提供商一起工作。

驱动程序

目前包含以下驱动程序

  • Clockwork
  • Nexmo
  • TextLocal
  • Twilio
  • AWS SNS(需要安装 aws/aws-sdk-php
  • 邮件(用于邮件到短信网关)
  • O2SK(O2 Slovakia)

此外,它还包括以下用于测试目的的驱动程序

  • RequestBin
  • Null
  • Log

RequestBin 将 POST 请求发送到指定的 RequestBin 路径以进行调试。Null 驱动程序不执行任何操作,而 Log 驱动程序接受 PSR3 日志记录器并将其用于记录请求。

示例用法

Null

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Matthewbdaly\SMS\Drivers\Null;
use Matthewbdaly\SMS\Client;

$guzzle = new GuzzleClient;
$resp = new Response;
$driver = new Null($guzzle, $resp);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'content' => 'Just testing',
];
$client->send($msg);

Log

use Matthewbdaly\SMS\Drivers\Log;
use Matthewbdaly\SMS\Client;
use Psr\Log\LoggerInterface;

$driver = new Log($logger); // $logger should be an implementation of Psr\Log\LoggerInterface
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'content' => 'Just testing',
];
$client->send($msg);

RequestBin

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Matthewbdaly\SMS\Drivers\RequestBin;
use Matthewbdaly\SMS\Client;

$guzzle = new GuzzleClient;
$resp = new Response;
$driver = new RequestBin($guzzle, $resp, [
    'path' => 'MY_REQUESTBIN_PATH',
]);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'content' => 'Just testing',
];
$client->send($msg);

Clockwork

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Matthewbdaly\SMS\Drivers\Clockwork;
use Matthewbdaly\SMS\Client;

$guzzle = new GuzzleClient;
$resp = new Response;
$driver = new Clockwork($guzzle, $resp, [
    'api_key' => 'MY_CLOCKWORK_API_KEY',
]);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'content' => 'Just testing',
];
$client->send($msg);

Nexmo

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Matthewbdaly\SMS\Drivers\Nexmo;
use Matthewbdaly\SMS\Client;

$guzzle = new GuzzleClient;
$resp = new Response;
$driver = new Nexmo($guzzle, $resp, [
    'api_key' => 'MY_NEXMO_API_KEY',
    'api_secret' => 'MY_NEXMO_API_SECRET',
]);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'from'    => 'Test User',
    'content' => 'Just testing',
];
$client->send($msg);

AWS SNS

use Matthewbdaly\SMS\Client;
use Matthewbdaly\SMS\Drivers\Aws;

$config = [
    'api_key'    => 'foo',
    'api_secret' => 'bar',
    'api_region' => 'ap-southeast-2'
];
$driver = new Aws($config);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'from'    => 'Test User',
    'content' => 'Just testing',
];
$client->send($msg);

邮件

use Matthewbdaly\SMS\Client;
use Matthewbdaly\SMS\Drivers\Mail;
use Matthewbdaly\SMS\Contracts\Mailer;

$config = [
    'domain' => 'my.sms-gateway.com'
];
$driver = new Mail($config);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'content' => 'Just testing',
];
$client->send($msg);

TextLocal

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Matthewbdaly\SMS\Drivers\TextLocal;
use Matthewbdaly\SMS\Client;

$guzzle = new GuzzleClient;
$resp = new Response;
$driver = new TextLocal($guzzle, $resp, [
    'api_key' => 'MY_TEXTLOCAL_API_KEY',
]);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'from'    => 'Test User',
    'content' => 'Just testing',
];
$client->send($msg);

Twilio

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Response;
use Matthewbdaly\SMS\Drivers\Twilio;
use Matthewbdaly\SMS\Client;

$guzzle = new GuzzleClient;
$resp = new Response;
$driver = new Twilio($guzzle, $resp, [
    'account_id' => 'MY_TWILIO_ACCOUNT_ID',
    'api_token' => 'MY_TWILIO_API_TOKEN',
]);
$client = new Client($driver);
$msg = [
    'to'      => '+44 01234 567890',
    'from'      => '+44 01234 567890',
    'content' => 'Just testing',
];
$client->send($msg);

O2SK

use GuzzleHttp\Client as GuzzleClient;
use Matthewbdaly\SMS\Drivers\O2SK;
use Matthewbdaly\SMS\Client;

$driver = new O2SK(new GuzzleClient, [
    'apiKey' => 'MY_O2SK_API_KEY',
]);
$client = new Client($driver);
$msg = [
    'message' => 'Testing message',
    'sender' => ['text' => 'Tester'],
    'recipients' => [
        ['phonenr' => '+421911000000']
    ]
];
$client->send($msg);

邮件驱动程序

我在 Matthewbdaly\SMS\Drivers\Mail 实现了一个邮件驱动程序,但它非常基本,可能无法直接与许多邮件到短信网关一起使用。它接受 Matthewbdaly\SMS\Contracts\Mailer 接口的实例作为第一个参数,并将配置数组作为第二个参数。

我已将 Matthewbdaly\SMS\PHPMailAdapter 类包含在库中,作为邮件接口的非常基本的实现,但它故意非常简单 - 它只是围绕 PHP mail() 函数的非常薄的包装。您几乎肯定会想要为您的特定用例创建自己的实现 - 例如,如果您正在使用 Laravel,您可能需要创建一个包装类以使用 Mail 门面。

邮件驱动程序几乎总是比基于 HTTP 的驱动程序慢且不可靠,因此如果您需要与尚未提供驱动程序但具有 REST API 的提供商集成,您可能最好为它创建一个 API 驱动程序。如果您确实需要与邮件到短信网关一起工作,您很可能会发现您需要扩展 Matthewbdaly\SMS\Drivers\Mail 以修正功能。

Laravel 和 Lumen 集成

使用 Laravel 或 Lumen?您可能想使用 我的集成包 而不是这个,因为那个包包含服务提供商,以及 SMS 门面和更简单的配置。

创建自己的驱动程序

创建自己的驱动程序非常简单 - 只需实现 Matthewbdaly\SMS\Contracts\Driver 接口。您可以使用发送短信的最合适的方法 - 例如,如果您的提供商具有邮件到短信网关,您可以在驱动程序中使用 Swiftmailer 或 PHPMailer 发送电子邮件,或者如果他们有 REST API,您可以使用 Guzzle。

您可以在驱动程序的构造函数中传递 config 数组中所需的任何配置选项。请确保您的驱动程序使用 PHPSpec 进行测试(请参阅现有驱动程序的示例),并且它符合编码标准(由于这个原因,该包包括 PHP Codesniffer 配置)。

如果您已创建新的驱动程序,请随时提交拉取请求,我将考虑将其包含在内。

待办事项

我计划发布 2.0 版本,其中包括

  • 更多驱动程序!如果您正在使用列表中未列出的短信服务提供商,并且希望在此库中看到对其的支持,请创建您自己的驱动程序,并为它提交一个拉取请求。
  • 移除对 Guzzle 的依赖,并使用 HTTPlug 替换它,这样就不需要特定的实现。
  • 添加一个用于自动解析驱动程序的工厂。