Skip to content

Integrations

Validate phone numbers and emails in PHP

Check emails, phone numbers and IPs from PHP with cURL. Reject undeliverable addresses, store E.164 numbers, flag risky traffic and retry on rate limits.

Updated 2026-11-23

This recipe checks an email, a phone number and an IP address from plain PHP with cURL. Drop the functions into any framework, or into a WordPress or Laravel project.

What you need

  • PHP 8.0 or later with the cURL and JSON extensions, both enabled in most installs.
  • A Qelka API key in the API_KEY environment variable, read with getenv('API_KEY'). A sk_test_ key never spends credits: see Test mode.
  • Server-side code only: see Authentication.

Steps

1. One function for every call

php
<?php

function qelka_call(string $path, array $body, int $maxAttempts = 4): array
{
    $retryable = [429, 500, 502, 503];
    for ($attempt = 1; ; $attempt++) {
        $retryAfter = 0;
        $curl = curl_init('https://api.qelka.com/v1' . $path);
        curl_setopt_array($curl, [
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 3,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_HTTPHEADER => [
                'Authorization: Bearer ' . getenv('API_KEY'),
                'Content-Type: application/json',
            ],
            CURLOPT_POSTFIELDS => json_encode($body),
            CURLOPT_HEADERFUNCTION => function ($handle, $line) use (&$retryAfter) {
                if (stripos($line, 'retry-after:') === 0) {
                    $retryAfter = (int) trim(substr($line, 12));
                }
                return strlen($line);
            },
        ]);
        $raw = curl_exec($curl);
        $status = (int) curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
        $transportError = curl_error($curl);
        curl_close($curl);

        if ($raw === false) {
            throw new RuntimeException('transport: ' . $transportError);
        }
        $data = json_decode($raw, true);
        if ($status >= 200 && $status < 300) {
            return $data;
        }
        if (in_array($status, $retryable, true) && $attempt < $maxAttempts) {
            sleep($retryAfter > 0 ? $retryAfter : min(2 ** ($attempt - 1), 16));
            continue;
        }
        $type = $data['error']['type'] ?? 'unknown';
        throw new RuntimeException($type . ': ' . ($data['error']['message'] ?? 'request failed'), $status);
    }
}

On a 429 the function waits for the Retry-After header, on 500 or 502 it backs off, and any other error is thrown at once. The message starts with the documented error type, such as insufficient_credits or invalid_api_key. See Errors and Rate limits.

2. Check a signup

php
function qelka_check_signup(string $email, string $phone, ?string $country, ?string $ip): array
{
    $errors = [];
    $checked = true;
    $e164 = null;
    $review = false;

    try {
        $result = qelka_call('/email', ['email' => $email]);
        if ($result['status'] === 'undeliverable') {
            $errors['email'] = $result['did_you_mean']
                ? 'Did you mean ' . $result['did_you_mean'] . '?'
                : 'This email address cannot receive mail.';
        } elseif ($result['status'] === 'risky' && $result['reason'] === 'disposable') {
            $review = true;
        }
    } catch (RuntimeException $error) {
        $checked = false;
        error_log('validation skipped: ' . $error->getMessage());
    }

    try {
        $body = ['phone' => $phone];
        if ($country) {
            $body['country'] = $country;
        }
        $result = qelka_call('/phone', $body);
        if ($result['status'] === 'valid') {
            $e164 = $result['e164'];
        } elseif ($result['status'] === 'invalid') {
            $errors['phone'] = 'This phone number is not valid.';
        }
    } catch (RuntimeException $error) {
        $checked = false;
        error_log('validation skipped: ' . $error->getMessage());
    }

    if ($ip) {
        try {
            $result = qelka_call('/ip', ['ip' => $ip]);
            $review = $review || $result['risk']['level'] === 'high' || $result['flags']['tor'];
        } catch (RuntimeException $error) {
            if (strpos($error->getMessage(), 'unprocessable_input') !== 0) {
                $checked = false;
                error_log('validation skipped: ' . $error->getMessage());
            }
        }
    }

    return ['ok' => count($errors) === 0, 'errors' => $errors, 'checked' => $checked, 'e164' => $e164, 'review' => $review];
}

Call it from your form handler and use the returned array:

php
$verdict = qelka_check_signup($_POST['email'], $_POST['phone'], $_POST['country'] ?? null, $_SERVER['REMOTE_ADDR'] ?? null);
if (!$verdict['ok']) {
    http_response_code(422);
    echo json_encode(['errors' => $verdict['errors']]);
    exit;
}

3. Read the result

Result Action
Email undeliverable Reject and show did_you_mean
Email risky Accept and flag, or reject disposable inboxes
Email unknown Accept and confirm by email
Phone valid Store e164 instead of the typed value
Phone unknown No + prefix and no country sent: send the country
IP risk high or Tor Flag for review

When checked is false, accept the signup and re-check from a queue. A 422 unprocessable_input on the IP means a private or loopback address, common in local development: ignore it. Behind a proxy or load balancer, REMOTE_ADDR is the proxy, so read the forwarded client address your proxy sets: see Flag VPN and data center traffic.

Frequently asked questions

Do I need Composer or a package?
No. PHP's built-in cURL extension is enough, and the example below uses nothing else. The API is plain HTTPS and JSON, so you can also use Guzzle or the Symfony HTTP client if your project already has one. Keep the Bearer header, the JSON body and the timeout, and the rest of the logic is the same.
Where should the API key live in PHP?
In an environment variable read with getenv, set in your server configuration, your .env loader or your host's secrets panel. Do not commit it, and do not expose it to templates or JavaScript. Use one key per service so you can revoke it alone, and use a test key in development.
What do I do when the API is unreachable?
Fail open. Set a connect and total timeout, catch the failure, let the person through and check again later from a queue. Block only on a clear negative result such as an undeliverable email or an invalid phone number. A validation service should never be the reason nobody can sign up.

Put this check in your product

Get an API key in under a minute. 100 free credits every month, no card required.

  • 100 free credits a month
  • No card
  • Test keys spend nothing
  • Cancel any time