craftsys / msg91-laravel
Laravel 服务提供者,用于发送 OTP、验证 OTP、重新发送 OTP、发送短信(短消息)等 Msg91 API
Requires
- php: ^7.1.3|^8.0|^8.1
- craftsys/msg91-php: ^0.15.3
- illuminate/support: ^5.2|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ~3.8|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^5.3|^6.0|^7.0|^8.0|^9.4|^10.0
Suggests
- craftsys/msg91-laravel-notification-channel: Easily send sms/otp notifications via msg91 channel
This package is auto-updated.
Last update: 2024-09-25 12:54:32 UTC
README
这是一个 laravel 服务提供者,用于 Msg91 APIs。它封装了 msg91-php 客户端,并通过暴露服务提供者和外观提供相同的功能。
目录
安装
该软件包可在 Packagist 上获取,并通过在 shell 中执行以下命令来使用 Composer 安装。
composer require craftsys/msg91-laravel
先决条件
- php^7.1
- laravel^5|^6|^7|^8|^9|^10
该软件包仅针对 5.8+、^6.0、^7.0、^8.0、^9.0、^10.0 进行了测试。如果您发现关于 Laravel (5.0 < 5.8) 的任何错误,请提交问题。
Laravel 5.5+
如果您正在使用 Laravel 5.5 或更高版本,该软件包将自动注册 Craftsys\Msg91\Msg91LaravelServiceProvider
提供者和别名 Craftsys\Msg91\Facade\Msg91
外观到 Msg91
。
Laravel 5.4 及以下
将 Craftsys\Msg91\Msg91LaravelServiceProvider
添加到您的 config/app.php
文件中的 providers
数组中
'providers' => [ // Other service providers... Craftsys\Msg91\Msg91LaravelServiceProvider::class, ],
如果您想使用外观接口,可以在需要时使用外观类
use Craftsys\Msg91\Facade\Msg91;
或者,在您的 config/app.php
中添加别名
'aliases' => [ // other aliases here 'Msg91' => Craftsys\Msg91\Facade\Msg91::class, ],
为了验证一切是否按预期工作,请在您的应用程序中某处执行以下 PHP 代码,例如在示例路由中或在 Laravel 中的 php artisan tinker
// this should print the `\Craftsys\Msg91\OTP\OTPService` of some default configuration values echo Msg91::otp()::class
如果存在问题,请再次检查步骤或提交支持问题。
配置
由于 msg91-php 提供的配置与 Laravel 的配置类似,因此该软件包只是将 Laravel 的配置迁移到 msg91-php 客户端。
可以通过在 config/services.php
配置文件中提供 msg91
键来配置该软件包。
<?php return [ // along with other services "msg91" => [ 'key' => env("Msg91_KEY"), ], ];
并更新 .env
文件以获取所需的值,例如 Msg91_KEY
。
请访问 msg91-php 配置 了解有关可用选项及其默认值的详细信息。
使用
一旦您已 配置 Laravel/Lumen 应用程序以使用服务提供者并将外观别名到 Msg91
,您将拥有一个 msg91-php 客户端 (Craftsys\Msg91\Client) 实例。
// send otp Msg91::otp()->to(919999999999)->send(); // resend otp Msg91::otp()->to(919999999999)->viaVoice()->resend(); // verify otp Msg91::otp(678612)->to(919999999999)->verify(); // send sms Msg91::sms()->to(919999999999)->flow('<flow_id>')->send(); // in bulk Msg91::sms()->to([919999999999, 918899898990])->flow('<flow_id>')->send(); // with variables in your flow template Msg91::sms()->to([919999999999, 918899898990])->flow('<flow_id>')->variable('variable_name', 'value')->send(); // with variables per recipient Msg91::sms()->recipients([ ['mobiles' => 919999999999, 'name' => 'Sudhir M'], ['mobiles' => 918899898990, 'name' => 'Craft Sys'] ]) ->flow('<flow_id>') ->send();
按照 示例 学习更多
示例
管理 OTP
发送、验证和重新发送等 OTP 服务可以通过客户端实例上的 otp
方法访问,例如 Msg91::otp()
。
有关详细使用说明,请访问msg91-php的OTP管理文档。
发送 OTP
Msg91::otp() ->to(912343434312) // phone number with country code ->template('your_template_id') // set the otp template ->send(); // send the otp
验证 OTP
Msg91::otp(1234) // OTP to be verified ->to(912343434312) // phone number with country code ->verify(); // Verify
重新发送 OTP
Msg91::otp() ->to(912343434312) // set the mobile with country code ->viaVoice() // set the otp sending method (can be "viaText" as well) ->resend(); // resend otp
发送短信
Msg91::sms() ->to(912343434312) // set the mobile with country code ->flow("your_flow_id_here") // set the flow id ->send(); // send
批量短信
Msg91::sms() ->to([912343434312, 919898889892]) // set the mobiles with country code ->flow("your_flow_id_here") // set the flow id ->send(); // send
消息变量
// send in bulk with variables Msg91::sms() ->to([912343434312, 919898889892]) // set the mobiles with country code ->flow("your_flow_id_here") // set the flow id ->variable('date', "Sunday") // the the value for variable "date" in your flow message template ->send(); // send // send in bulk with variables per recipient Msg91::sms() ->to([912343434312, 919898889892]) // set the mobiles with country code ->flow("your_flow_id_here") // set the flow id ->recipients([ ['mobiles' => 919999223345, 'name' => 'Sudhir M'], ['mobiles' => 912929223345, 'name' => 'Craft Sys'] ]) // (optionally) set a "date" variable for all the recipients ->variable('date', "Sunday") ->send(); // send
有关详细使用说明和选项,请访问msg91-php的短信发送文档。
处理响应
所有服务在成功响应时将返回\Craftsys\Msg91\Support\Response
实例,如果请求验证失败(\Craftsys\Msg91\Exceptions\ValidationException
)或响应中出现错误(\Craftsys\Msg91\Exceptions\ResponseErrorException
),则将抛出异常。
try { $response = $client->otp()->to(919999999999)->send(); } catch (\Craftsys\Msg91\Exceptions\ValidationException $e) { // issue with the request e.g. token not provided } catch (\Craftsys\Msg91\Exceptions\ResponseErrorException $e) { // error thrown by msg91 apis or by http client } catch (\Exception $e) { // something else went wrong // plese report if this happens :) }
有关所有示例和选项,请参阅msg91-php示例部分。
相关
致谢
我们感谢现有相关项目的作者们提供想法和合作。