androidneha/laravel-msg91

Msg91消息API的Laravel集成。

1.1.1 2019-10-09 03:40 UTC

This package is auto-updated.

Last update: 2024-09-05 18:38:52 UTC


README

关于

此包将MSG91的短信解决方案与Laravel 5完美集成,并增加了对通知验证器的支持。

注册

注册 MSG91并从您的账户中获取认证密钥。登录后,您可以在仪表板 > API密钥中找到认证密钥

安装

composer require androidneha/laravel-msg91

Laravel < 5.5

安装包后,打开您的app/config/app.php配置文件,找到providers键。将以下行添加到末尾

Laravel\Msg91\ServiceProvider::class

接下来,找到aliases键,并添加以下行

'Msg91' => Laravel\Msg91\Facade::class,

配置

使用MSG91_AUTH_KEYMSG91_DEFAULT_SENDERMSG91_DEFAULT_ROUTEMSG91_DEFAULT_COUNTRY密钥将凭证和首选项放入ENV中。如果您想自定义此设置,请发布默认配置,这将创建一个配置文件config/msg91.php

$ php artisan vendor:publish

使用方法

基本

  • 向一个或多个号码发送短信。
<?php

$result = Msg91::sms('919999999999', 'Hello there!');
 
$result = Msg91::sms('919999999999', 'Hello there!', 'TEST12');
 
$result = Msg91::sms(null, [
    ['to' => ['919999999999', '918888888888'], 'message' => 'Hello there!'],
    ['to' => ['917777777777'], 'message' => 'Come here!'],
], 'TEST12');
  • 向一个号码发送OTP。
<?php

$result = Msg91::otp('919999999999');
   
$result = Msg91::otp('919999999999', 'TEST12');
   
$result = Msg91::otp('919999999999', 'TEST12', '##OTP## is your OTP, Please dont share it with anyone.');
  • 验证发送到号码的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 Laravel\Msg91\Message\Msg91Message;

public function toMsg91()
{
    return (new Msg91Message)
        ->message(__('This is just a test message.'))
	->sender('MESG91') // [Optional] - Will pick default sender ID from MSG91_DEFAULT_SENDER or config
	->transactional(); // or promotional() [Optional] - Will pick default route from MSG91_DEFAULT_ROUTE or config
}

在可通知类中定义默认的routeNotificationForMsg91方法

<?php

public function routeNotificationForMsg91($notification)
{
    return $this->phone_number;
}

最后发送通知

<?php

$notifiable = /* some class */
$notifiable->notify(new App\Notifications\Msg91TestNotification());

对于向任意号码发送通知,请使用以下语法

<?php
use Illuminate\Support\Facades\Notification

Notification::route('msg91', '919876543210')
    ->notify(new App\Notifications\Msg91TestNotification());

验证器

您可以使用提供的名为msg91_otp的验证规则来验证发送的OTP,如下所示

<?php

use Illuminate\Support\Facades\Validator

$data = ['number' => '9876543210', 'otp' => '1234'];

$validator = Validator::make($data, [
    'number' => ['required', 'digits:10'],
    'otp' => ['required', 'digits:4', 'msg91_otp'], // default key for source number is 'number', you can customize this using 'msg91_otp:key_name'
]);

if ($validator->fails()) {
    // report errors
}

许可证

查看LICENSE文件。