全球短信 / smsglobal-php
SMSGlobal PHP 客户端库
v1.3.0
2021-06-21 07:30 UTC
Requires
- php: >=7.3
- guzzlehttp/guzzle: ^7.2
Requires (Dev)
- php-http/guzzle7-adapter: ^0.1.1
- phpunit/phpunit: >=8.0
README
SMSGlobal REST API 和 PHP 库
这是一个用于将 SMSGlobal REST API 集成到 PHP 应用程序的 PHP 客户端库。
今天注册一个 免费 SMSGlobal 账户 并从我们的高级短信平台 MXT 获取您的 API 密钥。此外,您可以享受无限的免费开发者沙盒测试,以全面测试您的 API!
要求
- PHP 7.3 以上
- Guzzle7 (PHP HTTP 客户端)
安装
要将 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(); }
发送 OTP
<?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
您的用户输入的 OTP 代码可以通过使用 requestId
或 目标号码
进行验证。以下是每种方法的示例
<?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 未过期且未验证,可以取消 OTP 请求。可以通过使用 requestId
或 目标号码
完成。以下是每种方法的示例
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" }
可用的 REST API 资源
- Sms
- Sms Incoming
- OTP (beta)
- User
单元测试
安装开发依赖项
composer require smsglobal/smsglobal-php
运行单元测试
./vendor/bin/phpunit tests
带覆盖率(需要 pcov 或 xdebug 扩展)
./vendor/bin/phpunit --coverage-text tests
获取帮助
查看 REST API 文档以获取可用资源的列表。
如有任何疑问,请 联系我们