gladyshev / rucaptcha-client
Lightweight and useful 2captcha.com/rucaptcha.com PHP-client with PSRs support.
Installs: 24 286
Dependents: 2
Suggesters: 0
Security: 0
Stars: 14
Watchers: 3
Forks: 4
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
- ext-mbstring: *
- guzzlehttp/psr7: *
- psr/http-client: ~1.0
- psr/log: ~1|~2|~3
Requires (Dev)
- guzzlehttp/guzzle: ~7.2
- monolog/monolog: 2|3
- phpunit/phpunit: 9.5.6
- squizlabs/php_codesniffer: 3.5.2
README
Удобная PHP-обёртка для сервиса распознавания капчи rucaptcha.com.
Оригинальная документация доступна по ссылке.
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;