Free Numbers Pricing Reviews Feedback Terms & Policy API Docs Blog About
Log in Get Started
1 views 3 min read
Back to Blog
Email API

Temporary Email API in PHP: The Full Flow From Domain List to Code

Temporary Email API in PHP: The Full Flow From Domain List to Code

PHP is still the language of a large share of websites, and the OtpGet email API fits it well, because every call is a simple GET that returns JSON. Here is the complete flow in one script, with the checks you need so that a failed order does not burn balance.

The script

<?php
const BASE = "https://otpget.com/stubs/email-handler.php";
const KEY  = "YOUR_API_KEY";
$site = "instagram.com";

function call(array $p): array {
    $p["api_key"] = KEY;
    $ch = curl_init(BASE . "?" . http_build_query($p));
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 30]);
    $body = curl_exec($ch);
    curl_close($ch);
    return json_decode($body ?: "[]", true) ?: ["status" => "0", "message" => "no answer"];
}

// 1. domains for this site
$list = call(["action" => "getEmailServices", "server" => 2, "site" => $site]);
if ($list["status"] !== "200") exit(json_encode($list));
usort($list["domains"], fn($a, $b) => $a["price"] <=> $b["price"]);
$domain = $list["domains"][0]["domain"];

// 2. buy
$order = call(["action" => "buyEmail", "server" => 2, "domain" => $domain, "site" => $site]);
if ($order["status"] !== "200") exit(json_encode($order));
echo "Use this address: " . $order["email"] . PHP_EOL;

// 3. poll for the code
$id = $order["email_id"];
$end = time() + 25 * 60;
while (time() < $end) {
    sleep(32);
    $r = call(["action" => "getEmailCode", "email_id" => $id]);
    if ($r["status"] === "200") { echo "Code: " . $r["messages"][0]["code"] . PHP_EOL; exit; }
    if ($r["status"] === "300") { echo "Cancelled" . PHP_EOL; exit; }
}
echo json_encode(call(["action" => "cancelEmail", "email_id" => $id])); // refund

Why it is written this way

  • The status is compared as a string, because the API returns it as one.
  • The timeout stops the script from hanging if the network is slow.
  • The loop has an end time and ends with a cancel, so you never leave an order open.
  • Domains are sorted by price so you start with the cheapest one.

Running it from a web page

Do not run a 25 minute loop inside a normal web request. Run it from the command line or a background worker, and keep your API key in a configuration file outside the public folder.

What to add next

  • A fallback list: if the cheapest domain is refused by the website, cancel and try the next one.
  • Logging of order ids and status, but never of the key.
  • A daily spending cap.

Putting it inside a project

Move the call() function into its own file and load the key from a configuration file outside the public web folder. Return the result to the rest of your application instead of echoing it, so a queue worker or an admin page can show the order. Keep the polling in a background process such as a cron script or a queue worker, and let the web page read the stored status.

Testing without spending much

  1. Add a small balance.
  2. Run the script once against a cheap domain.
  3. Check the order in Email History.
  4. Test the failure path by using a domain that is not offered for the site and confirm that your code handles the 404.
  5. Only then connect it to real work.

Questions people ask

Can I use file_get_contents instead of cURL?

Yes, but cURL gives you timeouts and better error handling.

Does it work for Server 1?

Yes with different parameters. See the cURL guide for both servers.

Related guides and pages

Create a free OtpGet account and try a temporary email with a small balance.

Tags

#temporary email api php #email otp api php #php curl email api #buy temp email php #otpget email api php #read verification code php
Contact us