nikkuang/sms-client

(适用于旧项目)

2.0.0 2022-06-28 12:55 UTC

This package is auto-updated.

Last update: 2024-09-28 18:00:12 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。

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

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

TODO

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

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

--- https://github.com/guzzle/guzzle/blob/master/UPGRADING.md