gladyshev / rucaptcha-client

CAPTCHAFORUM

Administrator
246d621b81[1].png

Convenient PHP wrapper for the captcha recognition service rucaptcha.com.
The original documentation is available here.

Install
$ composer require --prefer-dist gladyshev/rucaptcha-client "*"
or

"require": {
...
"gladyshev/rucaptcha-client": "*"
...
}
Examples
Code:
/* Simple */

$rucaptcha = new Rucaptcha\Client(
    Rucaptcha\Config::fromApiKey(getenv('__RUCAPTCHA_KEY__')),
    new GuzzleHttp\Client() // Any PSR-18 HTTP-client
);

$captchaText = $rucaptcha->recognizeFile('captcha.png');
print_r($captchaText); // h54g6

Code:
/* Advanced example */

$rucaptcha = new \Rucaptcha\Client(
    \Rucaptcha\Config::fromApiKey('YOUR_API_KEY'),
    new \GuzzleHttp\Client(['base_uri' => 'https://2captcha.com']),
    new \Monolog\Logger('2Captcha', [new \Monolog\Handler\StreamHandler('php://stdout')])
);

$taskIds = [];

$taskIds[] = $rucaptcha->sendCaptcha(file_get_contents('captcha1.png'));
$taskIds[] = $rucaptcha->sendCaptcha(file_get_contents('captcha2.jpg'));
$taskIds[] = $rucaptcha->sendCaptcha(file_get_contents('captcha3.gif'), [
    Rucaptcha\Extra::NUMERIC => 1
]);

$results = [];

while (count($taskIds) > 0)
{
    // Try get results
    foreach ($taskIds as $i=>$taskId)
    {   
        // Wait 5 sec
        sleep(5);
      
        $results[$taskId] = $rucaptcha->getCaptchaResult($taskId);
      
        // false === is not ready, on error we've got an exception
        if ($results[$taskId] === false) {
            continue;
        } else {
            unset($taskIds[$i]);
        }
    }
}

print_r($results);

Methods of Rucaptcha\Client
Code:
use Rucaptcha\Client;

/* Solving captcha methods */

Client::recognize(string $content, array $extra = []): string;
Client::recognizeFile(string $path, array $extra = []): string;
Client::sendCaptcha(string $content, array $extra = []): int;
Client::getCaptchaResult(int $captchaId): string;
Client::getCaptchaResultBulk(array $captchaIds): array;

/* Pingback stuff */

Client::addPingback(string $uri): void;
Client::getPingbacks(): array;
Client::deletePingback(string $uri): void;
Client::deleteAllPingbacks(): void;

/* Google Recaptcha V2 */

Client::sendRecapthaV2($googleKey, $pageUrl, $extra = []): int
Client::recognizeRecapthaV2($googleKey, $pageUrl, $extra = []): string

/* Other */

Client::getLastCaptchaId(): string;
Client::getBalance(): string;
Client::reportGood(string $captchaId): bool;
Client::reportBad(string $captchaId): bool;

Documentation https://github.com/gladyshev/rucaptcha-client