Captcha OCR

SDKs / PHP

PHP SDK

Package allvoraz/captcha-ocr, version 1.0.0. Requires PHP 8.1+ with ext-curl, ext-json, ext-mbstring (Guzzle 7).

Install

The SDK installs straight from our package server, captchaocr.allvoraz.com/sdk, with your usual tools. No public registry account is needed.

# Add our Composer repository once, then the package
composer config repositories.allvoraz composer https://captchaocr.allvoraz.com/sdk/composer
composer require allvoraz/captcha-ocr:^1.0.0

# composer.json gets:
#   "repositories": { "allvoraz": { "type": "composer", "url": "https://captchaocr.allvoraz.com/sdk/composer" } }

Prefer the code itself? Download the source (.zip).

Configure and solve

Create the client once with your API key (from Dashboard → API keys) and reuse it. Submit the answer only when reliable is true. Only reliable answers are charged.

<?php
require 'vendor/autoload.php';

use Allvoraz\CaptchaOcr\Api\SolveApi;
use Allvoraz\CaptchaOcr\Configuration;
use Allvoraz\CaptchaOcr\Model\SolveRequest;

$config = Configuration::getDefaultConfiguration()
    ->setHost('https://captchaocr.allvoraz.com')
    ->setAccessToken(getenv('CAPTCHA_API_KEY'));
$solver = new SolveApi(new GuzzleHttp\Client(), $config);

$image = base64_encode(file_get_contents('captcha.png'));
$res = $solver->solve(new SolveRequest(['image' => $image, 'numeric' => true, 'length' => 6]));
if ($res->getReliable()) {
    echo $res->getText(), PHP_EOL; // only reliable answers are charged
}

Options: numeric, uppercase, lowercase, length, math, hindi. See choosing options.

Batch

Up to 256 images in one call. Top-level options are shared, and each item can override them. Results keep the request order.

<?php
use Allvoraz\CaptchaOcr\Model\BatchItem;
use Allvoraz\CaptchaOcr\Model\BatchRequest;

$batch = $solver->solveBatch(new BatchRequest([
    'numeric' => true, // shared by every image
    'images' => [
        new BatchItem(['image' => b64('a.png')]),
        new BatchItem(['image' => b64('b.png'), 'length' => 5]), // per-image override
        new BatchItem(['image' => b64('sum.png'), 'math' => true]),
    ],
]));
foreach ($batch->getResults() as $r) { // same order as the request
    echo $r->getReliable() ? $r->getText() : 'refresh', PHP_EOL;
}

Submit now, fetch later

submit returns a task id immediately and solves in the background. getResult returns processing, then ready with the same answer solve returns (or failed, credit returned). Pass wait (up to 30 s) to hold the call until it's ready.

<?php
// Submit: returns a task id at once, the captcha is solved in the background.
$task = $solver->submit(new SolveRequest(['image' => $image, 'numeric' => true, 'length' => 6]));

// ... do other work ...

$r = $solver->getResult($task->getId(), 10); // waits up to 10 s
if ($r->getStatus() === 'ready' && $r->getResult()->getReliable()) {
    echo $r->getResult()->getText(), PHP_EOL;
}

Or poll without holding the connection:

<?php
// Poll without holding the connection.
while (($r = $solver->getResult($task->getId()))->getStatus() === 'processing') {
    usleep(500000);
}

Balance

<?php
$credits = (new Allvoraz\CaptchaOcr\Api\AccountApi(new GuzzleHttp\Client(), $config))->balance()->getCredits();

Low-balance alert

One alert per account: when a solve leaves your credits below threshold, the service POSTs your payload to your HTTPS url. It fires once and re-arms when you add credits. testAlert sends it now; getAlert and deleteAlert read and remove it. See how alerts work.

<?php
$account = new Allvoraz\CaptchaOcr\Api\AccountApi(new GuzzleHttp\Client(), $config);
echo $account->balance()->getCredits(), PHP_EOL;

// When credits drop below 500, POST this payload to your URL (one alert per account).
$account->setAlert(new Allvoraz\CaptchaOcr\Model\AlertRequest([
    'threshold' => 500,
    'url' => 'https://hooks.example.org/captcha-credits',
    'payload' => ['text' => 'Captcha credits low: {{credits}} left'],
    'headers' => ['Authorization' => 'Bearer my-hook-secret'],
]));
$test = $account->testAlert(); // sends it now
echo json_encode($test->getDelivered()), ' ', $test->getStatus(), PHP_EOL;
// $account->deleteAlert();    // remove it

Report a wrong answer

If the site rejected a reliable answer, report its id within 48 hours to get the credit back.

<?php
// The site rejected the answer: get the credit back (within 48 hours).
$r = (new Allvoraz\CaptchaOcr\Api\AccountApi(new GuzzleHttp\Client(), $config))
    ->report(new Allvoraz\CaptchaOcr\Model\ReportRequest(['id' => $res->getId()]));

Errors

HTTP errors (401 bad key, 402 no credits, 404 unknown task, 422 invalid options, 429 too many concurrent requests, 503 retry later) raise Allvoraz\CaptchaOcr\ApiException (getCode(), getResponseBody()). An undecodable image is not an exception: it's a normal result with error: "invalid_image". See errors.

More