byjg / sms-client
一个通用的、可扩展的轻量级短信客户端,用于发布Twilio和ByJG等短信服务提供商。
4.9.0
2024-01-05 23:27 UTC
Requires
- php: >=7.4.0
- ext-curl: *
- byjg/webrequest: 4.9.*
Requires (Dev)
- phpunit/phpunit: 5.7.*|7.4.*|^9.5
README
这是一个简单的客户端,用于使用不同的提供商发送短信。
功能
- 低代码发送短信
- 完全解耦的类
- 易于实现新的提供商
用法
使用ProviderFactory
// Register the provider and associate with a scheme ProviderFactory::registerProvider(TwilioMessagingProvider::class); // Create a provider $provider = ProviderFactory::create(new Uri("twilio://$accountSid:$authToken@default")); // Send a message $response = $byjg->send("12221234567", (new \ByJG\SmsClient\Message("This is a test message")->withSender("+12223217654")); // Check if the message was sent if ($response->isSent()) { echo "Message sent"; } else { echo "Message not sent"; }
使用ProviderFactory根据国家发送消息到多个提供商
// Register the provider and associate with a scheme ProviderFactory::registerProvider(TwilioMessagingProvider::class); ProviderFactory::registerProvider(ByJGSmsProvider::class); // Define the provider according to the country prefix ProviderFactory::registerServices("twilio://accoundId:authToken@default", ["+1"]); ProviderFactory::registerServices("byjg://username:password@default", ["+55"]); // Send a message and select the provider according to the country prefix $response = ProviderFactory::createAndSend("+5521900001234", (new \ByJG\SmsClient\Message("This is a test message"))); var_dump($response); $response = ProviderFactory::createAndSend("+12221234567", (new \ByJG\SmsClient\Message("This is a test message"))->withSender("+12223217654")); var_dump($response);
提供商
提供商是负责发送文本消息的类。
所有提供商都拥有以下接口
<?php interface ProviderInterface { public static function schema(); public function setUp(Uri $uri); public function send($to, Message $envelope): ReturnObject; }
没有必要调用方法 getConnection()
,因为方法 publish() 和 consume() 会自动调用它。只有在需要直接访问连接时,才使用方法 getConnection()
。
已实现提供商
安装
composer require "byjg/sms-client"