smsglobal php
v1.3.0
这是 SMSGlobal 的 REST API 的 PHP 客户端库,用于将 SMS 功能集成到您的 PHP 应用程序中。
立即注册免费 SMSGlobal 帐户,并从我们先进的 SMS 平台 MXT 获取您的 API 密钥。另外,享受无限制的免费开发者沙箱测试,以完整试用您的 API!
要将 PHP 客户端库安装到您的项目中,我们建议使用 Composer。
composer require smsglobal/smsglobal-php
查看examples
夹
发送短信
<?php
require_once __DIR__ . ' /vendor/autoload.php ' ;
// get your REST API keys from MXT https://mxt.smsglobal.com/integrations
SMSGlobal Credentials:: set ( ' YOUR_API_KEY ' , ' YOUR_SECRET_KEY ' );
$ sms = new SMSGlobal Resource Sms ();
try {
$ response = $ sms -> sendToOne ( ' DESTINATION_NUMBER ' , ' This is a test message. ' );
print_r ( $ response [ ' messages ' ]);
} catch ( Exception $ e ) {
echo $ e -> getMessage ();
}
发送一次性密码
<?php
require_once __DIR__ . ' /vendor/autoload.php ' ;
// get your REST API keys from MXT https://mxt.smsglobal.com/integrations
SMSGlobal Credentials:: set ( ' YOUR_API_KEY ' , ' YOUR_SECRET_KEY ' );
$ otp = new SMSGlobal Resource Otp ();
try {
$ response = $ otp -> send ( ' DESTINATION_NUMBER ' , ' {*code*} is your SMSGlobal verification code. ' );
print_r ( $ response );
} catch ( Exception $ e ) {
echo $ e -> getMessage ();
}
服务器将返回以下 json 响应:
{
"requestId" : " 404372541683674336263499 " ,
"validUnitlTimestamp" : " 2020-11-18 16:24:51 " ,
"createdTimestamp" : " 2020-11-18 16:22:51 " ,
"lastEventTimestamp" : " 2020-11-18 16:22:51 " ,
"destination" : " 61400000000 " ,
"status" : " Sent "
}
验证一次性密码
您的用户输入的 OTP 代码可以使用requestId
或destination number
进行验证。以下是每种方法的示例:
<?php
require_once __DIR__ . ' /vendor/autoload.php ' ;
// get your REST API keys from MXT https://mxt.smsglobal.com/integrations
SMSGlobal Credentials:: set ( ' YOUR_API_KEY ' , ' YOUR_SECRET_KEY ' );
$ otp = new SMSGlobal Resource Otp ();
try {
$ response = $ otp -> verifyByRequestId ( ' request Id ' , ' OTP code enterted by your user. ' );
print_r ( $ response );
} catch ( Exception $ e ) {
echo $ e -> getMessage ();
}
<?php
require_once __DIR__ . ' /vendor/autoload.php ' ;
// get your REST API keys from MXT https://mxt.smsglobal.com/integrations
SMSGlobal Credentials:: set ( ' YOUR_API_KEY ' , ' YOUR_SECRET_KEY ' );
$ otp = new SMSGlobal Resource Otp ();
try {
$ response = $ otp -> verifyByDestination ( ' destination number ' , ' OTP code enterted by your user. ' );
print_r ( $ response );
} catch ( Exception $ e ) {
echo $ e -> getMessage ();
}
如果验证成功,服务器会返回以下json响应:
{
"requestId" : " 404372541683674336263499 " ,
"validUnitlTimestamp" : " 2020-11-18 16:24:51 " ,
"createdTimestamp" : " 2020-11-18 16:22:51 " ,
"lastEventTimestamp" : " 2020-11-18 16:22:51 " ,
"destination" : " 61400000000 " ,
"status" : " Verified "
}
取消一次性密码
如果 OTP 尚未过期且尚未验证,则可以取消 OTP 请求。可以通过使用requestId
或destination number
来完成。以下是每种方法的示例:
require_once __DIR__ . ' /vendor/autoload.php ' ;
// get your REST API keys from MXT https://mxt.smsglobal.com/integrations
SMSGlobal Credentials:: set ( ' YOUR_API_KEY ' , ' YOUR_SECRET_KEY ' );
$ otp = new SMSGlobal Resource Otp ();
try {
$ response = $ otp -> cancelByRequestId ( ' request Id ' );
print_r ( $ response );
} catch ( Exception $ e ) {
echo $ e -> getMessage ();
}
require_once __DIR__ . ' /vendor/autoload.php ' ;
// get your REST API keys from MXT https://mxt.smsglobal.com/integrations
SMSGlobal Credentials:: set ( ' YOUR_API_KEY ' , ' YOUR_SECRET_KEY ' );
$ otp = new SMSGlobal Resource Otp ();
try {
$ response = $ otp -> cancelByDestination ( ' destination number ' );
print_r ( $ response );
} catch ( Exception $ e ) {
echo $ e -> getMessage ();
}
如果取消成功,服务器会返回以下json响应:
{
"requestId" : " 404372541683674336263499 " ,
"validUnitlTimestamp" : " 2020-11-18 16:24:51 " ,
"createdTimestamp" : " 2020-11-18 16:22:51 " ,
"lastEventTimestamp" : " 2020-11-18 16:22:51 " ,
"destination" : " 61400000000 " ,
"status" : " Cancelled "
}
安装开发依赖项
composer require smsglobal/smsglobal-php
运行单元测试
./vendor/bin/phpunit tests
具有覆盖范围(需要扩展 pcov 或 xdebug)
./vendor/bin/phpunit --coverage-text tests
查看 REST API 文档以获取可用资源的列表。
如有任何疑问请联系我们