matthewbdaly/sms-client

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

1.0.15 2020-08-07 12:14 UTC

This package is auto-updated.

Last update: 2024-09-07 21:21:07 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版本的计划,其中包括以下内容:

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