beboc / sms-client
一个通用的短信客户端库。支持多种可互换的驱动程序。
Requires
- guzzlehttp/guzzle: ^6.2
Requires (Dev)
- aws/aws-sdk-php: 3.*
- phpspec/phpspec: ^3.2
- psy/psysh: ^0.8.0
- squizlabs/php_codesniffer: ^2.7
Suggests
- aws/aws-sdk-php: Allows using the AWS SNS driver
README
一个通用的短信客户端库。支持多种可互换的驱动程序,因此您不必仅仅依赖于一个提供商。
此库旨在仅发送短信消息,我目前不计划添加其他功能。想法是创建一个库,它可以与任何有发送短信消息驱动程序的提供商一起工作。
驱动程序
目前包含以下驱动程序
- Clockwork
- Nexmo
- TextLocal
- Twilio
- AWS SNS(需要安装
aws/aws-sdk-php
) - 邮件(用于邮件到短信网关)
此外,它还包括以下测试用例的驱动程序
- RequestBin
- Null
- 日志
RequestBin将POST请求发送到指定的RequestBin路径进行调试。Null驱动程序不执行任何操作,而日志驱动程序接受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);
日志
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);
邮件驱动程序
我在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配置)。
如果您已创建一个新的驱动程序,请随时提交拉取请求,我会考虑将其包含在内。
待办事项
我计划发布2.0版本,包括以下内容
- 更多驱动程序!如果您正在使用不在列表上的短信服务提供商,并且希望在这个库中看到对其的支持,请创建您自己的驱动程序并提交一个pull请求。
- 移除对Guzzle的依赖,用HTTPlug替换,这样就不需要特定的实现。
- 添加一个用于自动解析驱动程序的工厂。