gladyshev/rucaptcha-client

Lightweight and useful 2captcha.com/rucaptcha.com PHP-client with PSRs support.

v2.1.0 2023-08-14 22:45 UTC

README

Удобная PHP-обёртка для сервиса распознавания капчи rucaptcha.com.
Оригинальная документация доступна по ссылке.

Build Status Code Coverage Scrutinizer Code Quality

Install

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

or

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

Examples

Больше примеров в папке examples.

/* 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
/* 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

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;

Client options

Solving options $extra