Unimatrix PHP SDK
Installation
Using Composer is the recommended way to install the Unimatrix SDK for PHP, which is available on Packagist.
Run the following command to add unimtx/uni-sdk
as a dependency to your project:
composer require unimtx/uni-sdk
Usage
The following example shows how to use the Unimatrix PHP SDK to interact with Unimatrix services.
Initialize a client
use Uni\UniClient;
$client = new UniClient([
'accessKeyId' => 'your access key id',
'accessKeySecret' => 'your access key secret' // if using simple auth mode, delete this line
]);
or you can configure your credentials by environment variables:
export UNIMTX_ACCESS_KEY_ID=your_access_key_id
export UNIMTX_ACCESS_KEY_SECRET=your_access_key_secret
Send SMS
Send a text message to a single recipient.
use Uni\UniClient;
use Uni\UniException;
$client = new UniClient();
try {
$resp = $client->messages->send([
'to' => '+1206880xxxx', // in E.164 format
'text' => 'Your verification code is 2048.'
]);
var_dump($resp->data);
} catch (UniException $e) {
print_r($e);
}
Send a message using a template with variables.
$client->messages->send([
'to' => '+1650253xxxx',
'signature' => 'Unimatrix',
'templateId' => 'pub_verif_en_basic2',
'templateData' => [
'code' => '2048'
]
]);
Send OTP
Send a one-time passcode (OTP) to a recipient. The following example will send a automatically generated verification code to the user.
use Uni\UniClient;
use Uni\UniException;
$client = new UniClient();
$resp = $client->otp->send([
'to' => '+1206880xxxx'
]);
var_dump($resp->data);
Verify OTP
Verify the one-time passcode (OTP) that a user provided. The following example will check whether the user-provided verification code is correct.
use Uni\UniClient;
use Uni\UniException;
$client = new UniClient();
$resp = $client->otp->verify([
'to' => '+1206880xxxx',
'code' => '123456' // the code user provided
]);
var_dump($resp->valid);