dejwcake/sms-client

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

2.0.0 2022-07-26 06:05 UTC

This package is auto-updated.

Last update: 2024-09-21 16:27:55 UTC


README

Build Status

这是一个通用的短信客户端库。支持多个可替换的驱动程序,这样你就不必只绑定到一个供应商。

这个库旨在仅用于发送短信消息,我并不打算添加其他功能的支持。想法是创建一个库,它应该能够与任何有发送短信消息驱动的供应商一起工作。

驱动程序

目前包含以下驱动程序

  • Clockwork
  • Nexmo
  • TextLocal
  • Twilio
  • AWS SNS(需要安装 aws/aws-sdk-php
  • Mail(用于邮件到短信网关)
  • 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);

Mail

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。

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

如果你已经创建了一个新的驱动程序,请随时提交一个pull请求,我会考虑将其包含在内。

待办事项

我计划发布2.0版本,包括

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