PHP captcha solver: an API client with cURL
A PHP captcha solver is a script that posts a challenge to a solver API and reads the answer, here with the cURL extension and json_encode. With ilovecaptcha it makes two HTTPS calls, createTask then getTaskResult, and needs no Composer package. This page walks through the request, the image, the wait and the errors, then gives the complete file.
Preview: the API described here is in public beta. Endpoint names, fields and codes can still change until the end of the beta, and no API key is distributed yet.
How do you call a captcha solver API from PHP?
With the cURL extension. The function post opens a handle with curl_init and configures it with curl_setopt_array: CURLOPT_POST, the header Content-Type:, the body from json_encode and a CURLOPT_TIMEOUT of 30 seconds. It then decodes the reply into an array with json_decode. The key is added to every body under clientKey.
| Item | Value |
|---|---|
| Runs on | PHP 8.0 or newer |
| Extensions | curl and json |
| Packages to install | None: no Composer dependency |
| File | solve.php |
| Command | php solve.php |
How do you send the captcha image and wait for the answer in PHP?
file_get_contents reads the image and base64_encode turns it into the string the task expects. solve posts the task to createTask, keeps taskId, then calls getTaskResult after each usleep until status is ready, and returns the solution array.
POLL_SECONDSis 1.5, converted to microseconds forusleep.TIMEOUT_SECONDSis 120: past that,solvethrows aRuntimeException.- The deadline is measured with
microtime. - A reply already
readyatcreateTaskis returned at once.
How do you handle the API key and errors in PHP?
Read the key with getenv from a variable such as ILOVECAPTCHA_KEY, set in the server configuration, instead of writing it in the constant API_KEY: after a configuration mistake, a PHP file can be served as plain text. post throws a RuntimeException in three cases: curl_exec returns false, the reply is not valid JSON, or errorId is not 0, in which case the message carries errorCode and errorDescription.
What does a complete PHP captcha solver client look like?
The file below is the whole client, in 58 lines. It declares post, solve and getBalance with typed signatures, then reads captcha.png, submits it as an aws-waf task and prints the solution with print_r. Replace YOUR_API_KEY and YOUR_API_URL with your own values and adapt the task array to your captcha before you run it.
solve.php · Complete PHP client: one createTask call, then getTaskResult until the task is ready (preview format).<?php
const API_KEY = "YOUR_API_KEY";
const BASE_URL = "YOUR_API_URL";
const POLL_SECONDS = 1.5;
const TIMEOUT_SECONDS = 120;
function post(string $route, array $body): array
{
$curl = curl_init(BASE_URL . $route);
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ["Content-Type: application/json"],
CURLOPT_POSTFIELDS => json_encode(["clientKey" => API_KEY] + $body),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$raw = curl_exec($curl);
if ($raw === false) {
throw new RuntimeException(curl_error($curl));
}
curl_close($curl);
$result = json_decode($raw, true);
if (!is_array($result)) {
throw new RuntimeException("invalid JSON from " . $route);
}
if (($result["errorId"] ?? 0) !== 0) {
throw new RuntimeException(($result["errorCode"] ?? "?") . ": " . ($result["errorDescription"] ?? ""));
}
return $result;
}
function solve(array $task): array
{
$result = post("/createTask", ["task" => $task]);
if (($result["status"] ?? "") === "ready") {
return $result["solution"];
}
$taskId = $result["taskId"];
$deadline = microtime(true) + TIMEOUT_SECONDS;
while (microtime(true) < $deadline) {
usleep((int) (POLL_SECONDS * 1000000));
$result = post("/getTaskResult", ["taskId" => $taskId]);
if (($result["status"] ?? "") === "ready") {
return $result["solution"];
}
}
throw new RuntimeException("task $taskId not ready after " . TIMEOUT_SECONDS . " s");
}
function getBalance(): float
{
return (float) post("/getBalance", [])["balance"];
}
$image = base64_encode(file_get_contents("captcha.png"));
print_r(solve(["type" => "aws-waf", "image" => $image]));
echo getBalance(), PHP_EOL;
PHP captcha solver: frequently asked questions
Do I need a Composer package or an SDK to solve captchas in PHP?
No. The API is plain HTTPS with a JSON body, and a standard PHP installation can call it: cURL sends the request, json_encode and json_decode handle the body and the reply, base64_encode prepares the image. The file on this page is the complete client. Include it with require_once and call solve: nothing is added to composer.json.
Does the PHP example work without the cURL extension?
Not as written, but the change is small. PHP can send the same POST request through file_get_contents with a stream context: set the method, the Content-Type header and the JSON body in the http options, and enable ignore_errors to read error replies. Only the function post changes. The json extension is always present in PHP 8.0 or newer, so json_encode and json_decode need no setup.
What does solve return in PHP, and how do you read it?
An associative array, because json_decode is called with true as its second argument. Its keys depend on the captcha: token for a token widget, points for an image grid, a list of x and y pairs in pixels of the submitted image, offset for a slider, text for characters to type. Check the key with isset before using it, since each task type returns its own.
Sources
The facts and figures quoted on this page come from these sources.
- The PHP Group, Client URL Library, PHP Manual (curl_init, curl_setopt_array, curl_exec).
- The PHP Group, json_encode, PHP Manual (JSON representation of a value).