anburocky3 / msg91-php
借助 MSG91 提供商,使用 PHP 无缝快速发送短信。
Requires
- php: ^7.4|^8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.0.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.17
- phpunit/phpunit: ^9.5
- spatie/ray: ^1.10
- vimeo/psalm: ^4.3
This package is auto-updated.
Last update: 2024-09-24 02:49:22 UTC
README
此库需要最低 PHP 版本 7.4
这是一个用于 Msg91 API 的 PHP 客户端。在使用之前,请确保您在 Msg91 上有账户并且有 Authkey(Msg91 仪表板 > API > 配置)。
注意:该项目正在积极开发中,因此,某些 API 可能会发生变化。
目录
安装
该包可在 Packagist 上找到,并且可以通过在壳中执行以下命令使用 Composer 安装。
composer require anburocky3/msg91-php
用法
如果您使用 Composer,请确保将自动加载器包含在项目的引导文件中
require_once "vendor/autoload.php";
现在可以通过将配置对象传递给构造函数来初始化客户端。
$config = [ 'key' => "123456789012345678901234", ]; $client = new Anburocky3\Msg91\Client($config);
该包在 Anburocky3\Msg91 命名空间下分发,如果您的项目处于命名空间环境中,则可以使用它。
<?php // in your use statement sections use Anburocky3\Msg91\Client; // somewhere in this source file where you need the client $client = new Client($config); // send OTP using the otp service on client $client->otp()->to(919999999999)->send(); // verify an OTP (1234) $client->otp(1234)->to(919999999999)->verify(); // send SMS using the sms service on client $client->sms()->to(919999999999)->flow("<flow_id>")->send();
继续阅读以了解有关配置和 opt 和 sms 服务的可用方法的更多信息,包括示例。
创建客户端
客户端负责与 Msg91 API 交互。
$client = new Anburocky3\Msg91\Client($config);
也可以不传递配置来初始化客户端,这可以通过在客户端实例上调用 setConfig($config) 方法来设置。
$client = new Anburocky3\Msg91\Client(); $client->setConfig($config);
您还可以将自定义的 GuzzleHttp\Client 作为构造函数的第二个参数传递给客户端。
$client = new Anburocky3\Msg91\Client($config, new GuzzleHttp\Client());
配置
该模块可配置以符合您的特定需求,其中您需要为 API 设置默认选项,例如默认 OTP 消息格式、重试方法等。
一个示例配置可能如下所示
$config = [ 'key' => "123456789012345678901234", 'otp_message' => "G: ##OTP## is your verification code". ];
以下配置选项可用
注意:将这些值设置为 null 将覆盖默认值为 null。因此,将使用 Msg91 API 的默认值。例如,将 otp_message 设置为 null 将允许使用 "您的验证码是 ##OTP##",这是 API 的默认值。
OTP 和短信服务
创建客户端后,您可以通过访问 otp 和 sms 服务来发送 OTP 和短信。
注意:必须在客户端使用任何其他服务之前设置配置。
// send otp $client->otp()->to(919999999999)->send() // verify a given otp $client->otp(<otp_to_be_verified>)->to(919999999999)->verify() // resend otp via voice channel $client->otp()->to(919999999999)->viaVoice()->resend() // send sms $client->sms()->to(919999999999)->flow("<flow_id>")->send();
接下来,请按照 示例 学习更多
示例
管理 OTP
可以通过客户端实例上的 otp 方法访问发送、验证和重发等 OTP 服务,例如 $client->otp()。所有可用的 Msg91 API 参数都有相应的直观方法名称,例如,要设置发送 OTP 请求中 OTP 的数字,您可以在服务上调用 digits 方法。您可以通过 \Anburocky3\Msg91\Options 类创建这些方法。请参阅以下示例以了解更多信息。
发送 OTP
基本用法
$otp = $client->otp(); // an OTP (optional) can be passed to service if you are generating otp on your end // $otp = $client->otp(4657); $otp->to(912343434312) // phone number with country code ->send(); // send the otp
高级用法
您可以使用服务中的options辅助工具来传递Msg91 API接受的全部自定义选项,而不是依赖Msg91或客户端的默认设置。此方法接受一个会调用底层的\Anburocky3\Msg91\OTP\Options实例的闭包,并赋予您添加所需任何选项的完全灵活性。
$otp->to(91123123123) ->from("SENDER_ID") // sender id ->template("AFH") // template id for otps ->options(function (\Anburocky3\Msg91\OTP\Options $options) { $options->digits(6) // set the number of digits in generated otp ->message("##OTP## is your verification code") // custom template ->expiresInMinutes(60); // set the expiry }) ->send() // finally send
注意:如果您在本地生成OTP并将其与自定义消息一起传递到服务中,您必须在消息中包含##OTP##或OTP的实际值。否则会导致错误。
// OK $client->otp(123242)->message('Your OTP is: 123242')->send(); // NOT OK! $client->otp(123123)->message("Use this for verification")->send(); // This will result in error with "Message doesn't contain otp"
验证 OTP
由于验证不会发送任何消息,您只需要提供验证OTP所需的字段,例如发送的OTP和手机号码。
$otp = $client->otp(12345); // OTP to be verified $otp->to(912343434312) // phone number with country code ->verify(); // Verify
重发 OTP
要重新发送OTP,访问otp服务并调用resend方法以重新发送OTP。在发送OTP之前,可以通过调用viaVoice或viaText来更改通信方法。默认值可以在配置中设置。
$otp = $client->otp(); $otp->to(912343434312) // set the mobile with country code ->viaText() // or ->viaVoice() ->resend(); // resend otp
发送短信
要发送短信,通过在客户端实例上调用->sms()方法访问SMSService。
$sms = $client->sms(); $sms->to(912343434312) // set the mobile with country code ->flow('flow_id_here') // set the flow id ->message("You have 10 pending tasks for the end of the day") // message content ->send(); // send the message
要向消息添加更多选项,您可以在发送消息之前调用options方法。选项方法接受一个调用,该调用将接收一个\Anburocky3\Msg91\SMS\Options实例。使用它,您可以修改任何所需的选项。
$client->sms() ->to(919999999999) ->flow('flow_id_here') // set the flow id ->options(function ($options) { $options->transactional() // set that it is a transactional message ->from('CMPNY') // set the sender ->unicode(); // handle unicode as the message contains unicode characters }) ->message("I ❤️ this package. Thanks.") ->send();
消息变量
要发送短信,您创建一个Flow,其中您可以提供短信模板。该模板可能包含一些变量,您将其用作每条消息数据的占位符。您可以使用recipients方法在发送短信时为每个收件人设置这些变量的值,如下所示。
假设您已创建以下消息模板的Flow
Hi ##name##, Your reward credits will expire on ##date##.
Claim them to get exclusive benefits
我们将为每个收件人更新name和date变量。
$client->sms() ->flow('flow_id_here') // set the flow id ->recipients([ ['mobiles' => '919999999999', 'name' => 'Sudhir M', 'date' => '20 Jan, 2022'], ['mobiles' => '919898912312', 'name' => 'Craft Sys', 'date' => '25 Jan, 2022'] ]) ->options(function ($options) { $options->receiverKey('mobiles'); // set your receiver key }) ->send();
收件人数组中的mobiles键来自创建新Flow时设置的receiver字段的值,默认为mobiles。如果您提供了不同的receiver键,请相应地更新recipients键。
如果您想为所有收件人设置相同变量的值,可以使用variable方法如下
$client->sms() ->flow('flow_id_here') // set the flow id ->to([919898912312, 912343434312]) // you can pass an array of mobile numbers ->variable('overdue', 'Yes') // set the "overdue" variable's value to "Yes" ->variable('url', 'https://domain.com') // set the "url" variable's value to "https://domain.com" ->send(); // you can also combine this with existing variables if you want e.g. $client->sms() ->flow('flow_id_here') // set the flow id ->recipients([ ['mobiles' => '919999999999', 'name' => 'Sudhir M', 'date' => '20 Jan, 2022'], ['mobiles' => '919898912312', 'name' => 'Craft Sys', 'date' => '25 Jan, 2022'] ]) ->variable('overdue', 'Yes') // will set the "overdue" for all recipients ->send();
注意:您应该在设置收件人之后调用variable方法。
接收者密钥
在MSG91上创建Flow时,您可以创建一个自定义的receiver键,默认为##mobiles##。此默认值也在此包中使用。如果您已使用不同的receiver键配置了您的流程,您可以通过以下方式设置它
假设您已将receiver字段设置为##contact##,
$client->sms() ->to(919999999999) ->flow('flow_id_here') // set the flow id ->options(function ($options) { $options->receiverKey('contact'); // your receiver key }) ->send();
处理响应
所有服务都将返回\Anburocky3\Msg91\Support\Response实例,对于所有成功响应,如果发生异常,则抛出异常
\Anburocky3\Msg91\Exceptions\ValidationException:请求验证失败\Anburocky3\Msg91\Exceptions\ResponseErrorException:响应中存在错误
try { $response = $client->otp()->to(919999999999)->send(); $response->getData(); // response data $response->getMessage(); // response message $response->getStatusCode(); // response status code } catch (\Anburocky3\Msg91\Exceptions\ValidationException $e) { // issue with the request e.g. token not provided } catch (\Anburocky3\Msg91\Exceptions\ResponseErrorException $e) { // error thrown by msg91 apis or by http client } catch (\Exception $e) { // please report if this happens :) // something else went wrong }
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
致谢
主要源代码参考自以下
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。