1. Go to this page and download the library: Download otp-id/otp-id-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
otp-id / otp-id-php example snippets
tpId\Channel;
use OtpId\Client;
$client = new Client(getenv('OTPID_API_KEY'));
// 1. Send an OTP (code generated by OTP.ID, never returned to you).
$res = $client->requestOtp([
'channel' => Channel::WHATSAPP,
'destination' => '6281234567890',
'brand' => 'MyApp', // shown in the OTP message; defaults to your merchant brand_name
'external_id' => 'order-8821', // optional idempotency key
]);
echo "otp_id: {$res->otpId} balance: {$res->lastBalance}\n";
// 2. Later, verify what the user typed.
$v = $client->verifyOtp($res->otpId, '482913');
if ($v->verified) {
echo "verified!\n";
} else {
echo "wrong code: {$v->reason}\n"; // "mismatch" — not an error
}
use OtpId\ErrorCode;
use OtpId\Exception\ApiException;
try {
$res = $client->requestOtp($params);
} catch (ApiException $e) {
switch ($e->getErrorCode()) {
case ErrorCode::INSUFFICIENT_BALANCE:
// top up first
break;
case ErrorCode::DUPLICATE_EXTERNAL_ID:
$existing = $e->getDetails()['existing_otp_id'] ?? null; // recover the original transaction
break;
case ErrorCode::RATE_LIMITED:
// slow down (20 requests per second per API key)
break;
}
} catch (\OtpId\Exception\OtpIdException $e) {
// ApiException (see above) or ConnectionException — a network-layer
// failure (DNS, connection refused, TLS, timeout).
}