PHP Examples

PHP examples for integrating the nofakemails email validation API using native cURL.

Single Email Validation

Validate a single email address with a GET request to the /v1/email/{email} endpoint.

validate-email.php
$email = 'user@example.com';
$apiKey = getenv('NOFAKEMAILS_API_KEY');

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => 'https://api.nofakemails.com/v1/email/' . urlencode($email),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'X-API-Key: ' . $apiKey,
    ],
]);

$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if ($httpCode !== 200) {
    echo "API error (status $httpCode)\n";
    exit(1);
}

$result = json_decode($response, true);
print_r($result);

Response:

{
  "email": "user@example.com",
  "valid": true,
  "normalized": "user@example.com",
  "local_part": "user",
  "alias": false,
  "ascii": true,
  "role": null,
  "gibberish": false,
  "gibberish_score": 0.12,
  "domain": "example.com",
  "mx": true,
  "disposable": false,
  "public": false,
  "relay": false,
  "typo": false,
  "typo_suggestion": null
}

See the Response Format page for a full description of every field.

Batch Validation

Validate up to 64 email addresses in a single POST request to the /v1/emails endpoint.

batch-validate.php
$emails = [
    'user1@example.com',
    'user2@gmail.com',
    'test@tempmail.com',
];

$apiKey = getenv('NOFAKEMAILS_API_KEY');

$curl = curl_init('https://api.nofakemails.com/v1/emails');

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'X-API-Key: ' . $apiKey,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode(['emails' => $emails]),
]);

$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

if ($httpCode !== 200) {
    echo "API error (status $httpCode)\n";
    exit(1);
}

$batch = json_decode($response, true);

// Check for partial results (timeout)
if ($batch['partial']) {
    echo "Warning: some emails may not have been processed\n";
}

echo "Total: {$batch['total']}, Valid: {$batch['valid_count']}\n";

foreach ($batch['results'] as $item) {
    if ($item['status'] === 'error') {
        echo "{$item['email']}: ERROR - {$item['error']['message']}\n";
        continue;
    }

    $r = $item['result'];
    echo "{$r['email']}: valid={$r['valid']}, disposable={$r['disposable']}\n";
}

Response:

{
  "success": true,
  "partial": false,
  "total": 3,
  "valid_count": 3,
  "invalid_count": 0,
  "error_count": 0,
  "usage_points": 3,
  "processing_time_ms": 198,
  "results": [
    {
      "email": "user1@example.com",
      "status": "success",
      "result": {
        "email": "user1@example.com",
        "valid": true,
        "normalized": "user1@example.com",
        "local_part": "user1",
        "alias": false,
        "ascii": true,
        "role": null,
        "gibberish": false,
        "gibberish_score": 0.11,
        "domain": "example.com",
        "mx": true,
        "disposable": false,
        "public": false,
        "relay": false,
        "typo": false,
        "typo_suggestion": null
      },
      "error": null
    },
    {
      "email": "user2@gmail.com",
      "status": "success",
      "result": {
        "email": "user2@gmail.com",
        "valid": true,
        "normalized": "user2@gmail.com",
        "local_part": "user2",
        "alias": false,
        "ascii": true,
        "role": null,
        "gibberish": false,
        "gibberish_score": 0.10,
        "domain": "gmail.com",
        "mx": true,
        "disposable": false,
        "public": true,
        "relay": false,
        "typo": false,
        "typo_suggestion": null
      },
      "error": null
    },
    {
      "email": "test@tempmail.com",
      "status": "success",
      "result": {
        "email": "test@tempmail.com",
        "valid": true,
        "normalized": "test@tempmail.com",
        "local_part": "test",
        "alias": false,
        "ascii": true,
        "role": null,
        "gibberish": false,
        "gibberish_score": 0.10,
        "domain": "tempmail.com",
        "mx": true,
        "disposable": true,
        "public": false,
        "relay": false,
        "typo": false,
        "typo_suggestion": null
      },
      "error": null
    }
  ]
}

Each item in results contains the full validation result. See the Batch Validation response format for details on the envelope fields and per-email result structure.