PHP code example of gladyshev / rucaptcha-client

1. Go to this page and download the library: Download gladyshev/rucaptcha-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gladyshev / rucaptcha-client example snippets


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

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

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;