saber13812002 / sms
Laravel SMS网关集成包
Requires
- php: ^7.4|^8.0
- guzzlehttp/guzzle: ^7.0
- illuminate/http: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
Requires (Dev)
- aws/aws-sdk-php: ^3.87
- friendsofphp/php-cs-fixer: ^3.0
- kavenegar/php: ^1.2
- laravel/legacy-factories: ^1.0
- mediaburst/clockworksms: ^2.0
- melipayamak/php: ^1.0.0
- orchestra/testbench: ^5.0|^6.0
- phpunit/phpunit: ^9.3
- smsgatewayme/client: ^0.0.1
- twilio/sdk: ^6.0
- vimeo/psalm: ^4.0
README
这是一个用于Laravel SMS网关集成的包。现在发送短信变得简单。
支持的网关列表
-
其他正在开发中。
📦 安装
通过Composer
$ composer require tzsk/sms
⚡ 配置
发布配置文件
$ php artisan sms:publish
在配置文件中,您可以设置默认的驱动程序,用于所有短信。但您也可以在运行时更改驱动程序。
选择您想要在应用程序中使用的网关。然后将其作为默认驱动程序,这样您就不需要在每个地方都指定。
// Eg. if you wan to use SNS. 'default' => 'sns',
然后在驱动程序数组中填写该网关的凭据。
// Eg. for SNS. 'drivers' => [ 'sns' => [ // Fill all the credentials here. 'key' => 'Your AWS SNS Access Key', 'secret' => 'Your AWS SNS Secret Key', 'region' => 'Your AWS SNS Region', 'sender' => 'Your AWS SNS Sender ID', 'type' => 'Tansactional', // Or: 'Promotional' ], ... ]
Textlocal配置
Textlocal默认添加。您只需更改textlocal驱动程序部分的凭据即可。
AWS SNS配置
如果您想使用AWS SNS,那么您首先需要拉取一个Composer库。
composer require aws/aws-sdk-php
Clockwork配置
如果您想使用Clockwork,那么您首先需要拉取一个Composer库。
composer require mediaburst/clockworksms
Twilio配置
如果您想使用Twilio,那么您首先需要拉取一个Composer库。
composer require twilio/sdk
然后您只需更改twilio驱动程序部分的凭据即可。
Melipayamak配置
如果您想使用Melipayamak,那么您首先需要拉取一个Composer库。
composer require melipayamak/php
Kavenegar配置
如果您想使用Kavenegar,那么您首先需要拉取一个Composer库。
composer require kavenegar/php
SMS Gateway Me配置
如果您想使用SMS Gateway Me,那么您首先需要拉取一个Composer库。
composer require smsgatewayme/client
🔥 使用
在您的代码中,只需像这样使用即可。
# On the top of the file. use Tzsk\Sms\Facades\Sms; ... # In your Controller. Sms::send("this message", function($sms) { $sms->to(['Number 1', 'Number 2']); # The numbers to send to. }); # OR... Sms::send("this message")->to(['Number 1', 'Number 2'])->dispatch(); # If you want to use a different driver. Sms::via('gateway')->send("this message", function($sms) { $sms->to(['Number 1', 'Number 2']); }); # OR... Sms::via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch(); # Here gateway is explicit : 'twilio' or 'textlocal' or any other driver in the config. # The numbers can be a single string as well. # If you are not a Laravel's facade fan, you can use sms helper: sms()->send("this message", function($sms) { $sms->to(['Number 1', 'Number 2']); # The numbers to send to. }); sms()->send("this message")->to(['Number 1', 'Number 2'])->dispatch(); sms()->via('gateway')->send("this message", function($sms) { $sms->to(['Number 1', 'Number 2']); }); sms()->via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();
😍 通道使用
首先,您必须使用php artisan make:notification命令创建通知。然后,可以像下面这样使用SmsChannel::class作为通道
namespace App\Notifications; use Tzsk\Sms\Builder; use Illuminate\Bus\Queueable; use Tzsk\Sms\Channels\SmsChannel; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; class InvoicePaid extends Notification { use Queueable; /** * Get the notification channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return [SmsChannel::class]; } /** * Get the repicients and body of the notification. * * @param mixed $notifiable * @return Builder */ public function toSms($notifiable) { return (new Builder)->via('gateway') # via() is Optional ->send('this message') ->to('some number'); } }
提示:您可以在发送方法中使用相同的Builder实例。
$builder = (new Builder)->via('gateway') # via() is Optional ->send('this message') ->to('some number'); Sms::send($builder); # OR... $builder = (new Builder)->send('this message') ->to(['some number']); Sms::via('gateway')->send($builder);
自定义驱动程序,如何实现
首先,您必须在驱动程序数组中命名您的驱动程序,并可以指定您想要的任何配置参数。
'drivers' => [ 'textlocal' => [...], 'twilio' => [...], 'my_driver' => [ ... # Your Config Params here. ] ]
现在,您必须创建一个驱动程序映射类,该类将用于发送短信。在您的驱动程序中,您只需扩展Tzsk\Sms\Contracts\Driver。
例如:您创建了一个类:App\Packages\SMSDriver\MyDriver。
namespace App\Packages\SMSDriver; use Tzsk\Sms\Contracts\Driver; class MyDriver extends Driver { # You will have to make 2 methods. /** * 1. __constructor($settings) # {Mandatory} This settings is your Config Params that you've set. * 2. send() # (Mandatory) This is the main message that will be sent. * * Example Given below: */ /** * @var array */ protected $settings; /** * @var mixed */ protected $client; /** * Your Driver Config. * * @var array $settings */ public function __construct($settings) { $this->settings = $settings; # Initialize any Client that you want. $this->client = new Client(); # Guzzle Client for example. } /** * @return object Ex.: (object) ['status' => true, 'data' => 'Client Response Data']; */ public function send() { $this->recipients; # Array of Recipients. $this->body; # SMS Body. # Main logic of Sending SMS. ... } }
一旦创建了该类,您必须在sms.php配置文件的map部分中指定它。
'map' => [ ... 'my_driver' => App\Packages\SMSDriver\MyDriver::class, ]
注意:您必须确保map数组的键与drivers数组的键相同。
🔬 测试
composer test
📅 更新日志
请参阅更新日志以获取有关最近更改的更多信息。
❤️ 贡献
请参阅贡献以获取详细信息。
🔒 安全漏洞
请查阅我们如何报告安全漏洞的安全策略。
👑 致谢
👮♂️ 许可证
MIT 许可证(MIT)。请参阅许可证文件获取更多信息。