WhatsApp OTP Example
Send WhatsApp OTP with PHP
A PHP cURL example for server-side WhatsApp OTP sending.
Setup
- Enable cURL in PHP.
- Store DNZ API settings outside the public web root.
Code
$payload = [
"apiKey" => getenv("WHATSAPP_OTP_API_KEY"),
"number" => "+9647878785391",
"message" => "Your verification code is 482910",
];
$ch = curl_init(getenv("WHATSAPP_OTP_ENGINE_URL") . "/api/send-otp");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status >= 400) {
throw new Exception("DNZ WhatsApp OTP failed: " . $body);
}Production notes
- Validate the phone number before sending.
- Hash and expire OTP values in your database.
