vaibhavpandeyvpz / msg91
使用 MSG91 发送 SMS/OTP 的 PHP 客户端。
1.4
2022-08-25 04:35 UTC
Requires
- php: >=7.1
- ext-json: *
- guzzlehttp/guzzle: ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- laravel/framework: ^5.0 || ^6.0 || ^7.0 || ^8.0
README
使用 MSG91 发送 SMS/OTP 的 PHP 客户端,包括对 Laravel 的可选支持。
安装
composer require vaibhavpandeyvpz/msg91
Laravel < 5.5
安装包后,打开你的 app/config/app.php
配置文件,找到 providers
键。在末尾添加以下行
Msg91\Laravel\ServiceProvider::class
接下来,找到 aliases
键并添加以下行
'Msg91' => Msg91\Laravel\Facade::class,
配置
你需要在项目的 .env
文件中添加 MSG91_KEY
。你也可以使用以下命令将默认配置文件发布为 config/msg91.php
。
$ php artisan vendor:publish
用法
基本
- 向一个或多个号码发送短信。
<?php // send an SMS to one number $result = Msg91::sms( '919999999999', 'Hello there!', 'MSGIND', 4 /* 1 = Promotional; 4 = Transactional */, [ 'DLT_TE_ID' => '<dlt-registered-template-id>', ] ); // send same/different SMS to multiple numbers $result = Msg91::sms( null, [ ['to' => ['919999999999', '918888888888'], 'message' => 'Hello fellas!'], ['to' => ['917777777777'], 'message' => 'Hello vpz!'], ], 'MSGIND', 4 /* 1 = Promotional; 4 = Transactional */, [ 'DLT_TE_ID' => '<dlt-registered-template-id>', ] );
- 向一个号码发送 OTP。
<?php $result = Msg91::otp('919999999999', 'MSGIND', [ 'template_id' => '<msg91-approved-otp-template-id>', ]);
- 将 OTP(作为语音)重试到号码。
<?php $result = Msg91::retry('919999999999', true); // returns true or false
- 验证发送到号码的 OTP。
<?php $result = Msg91::verify('919999999999', 1290); // returns true or false
通知
将 msg91
包含在通知的通道中
<?php /** * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['msg91']; }
定义 toMsg91
方法
<?php use Msg91\Laravel\Notification\Message as Msg91Message; public function toMsg91() { return (new Msg91Message) ->message(__('This is just a test message.')) ->sender('MSGIND') ->transactional() ->options([ 'DLT_TE_ID' => '<dlt-registered-templated-id>', ]); }
在你的可通知类中实现 routeNotificationForMsg91
方法
<?php public function routeNotificationForMsg91($notification) { return $this->phone; }
最后发送通知
<?php $notifiable = /* some class */ $notifiable->notify(new App\Notifications\Msg91TestNotification());
对于向任意号码发送通知,使用以下语法
<?php use Illuminate\Support\Facades\Notification; Notification::route('msg91', '919999999999') ->notify(new App\Notifications\Msg91TestNotification());
验证器
你可以使用提供的名为 msg91_otp
的验证规则来验证发送的 OTP,如下所示
<?php use Illuminate\Support\Facades\Validator; $data = ['phone' => '919999999999', 'otp' => '1234']; $validator = Validator::make($data, [ 'phone' => ['required', 'digits_between:10,12'], 'otp' => ['required', 'digits:4', 'msg91_otp'], // default key for source number is 'phone', you can customize this using 'msg91_otp:key_name' ]); if ($validator->fails()) { // report errors }
许可证
查看 LICENSE 文件。