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 文件以取得可用資源的清單。
如有任何疑問請聯絡我們