cURL के साथ PHP में captcha solver: working उदाहरण (2026)
PHP को captcha solve करने के लिए केवल cURL extension चाहिए, जो ज़्यादातर installs में पहले से होता है। आप Scarnox API को एक JSON POST भेजते हैं, response से solution.token पढ़ते हैं, और उस token को site के form में submit करते हैं। यह guide PopularCaptcha के लिए पूरा PHP flow दिखाती है, जिसे Scarnox आज proxyless आधार पर solve करता है।
मुफ़्त ट्रायल क्रेडिट से शुरू करें, कार्ड की ज़रूरत नहीं और script को किसी और चीज़ पर इस्तेमाल करने से पहले अपने किसी page पर आज़माएँ।
ज़रूरतें
- PHP 8.0 या नया, जिसमें
curlऔरjsonextensions चालू हों।php -mसे जाँचें। - Scarnox API key।
- Page के PopularCaptcha widget की sitekey। उसे ढूँढने में मदद के लिए captcha sitekey कैसे खोजें देखें।
Solver function
यह function API पर POST करता है, errorId जाँचता है, और token लौटाता है। किसी भी विफलता पर वह exception throw करता है, ताकि caller तय कर सके कि retry करना है या नहीं:
<?php
declare(strict_types=1);
const SCARNOX_API_KEY = 'scarnox_YOURAPIKEY';
const SCARNOX_API_URL = 'https://api.scarnox.com/api/tasks/create';
function solveCaptcha(string $sitekey, string $pageUrl, bool $invisible = false): string
{
$body = [
'captcha_type' => 'PopularCaptchaTaskProxyless',
'site_key' => $sitekey,
'site_url' => $pageUrl,
];
if ($invisible) {
$body['is_invisible'] = true;
}
$ch = curl_init(SCARNOX_API_URL);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($body, JSON_THROW_ON_ERROR),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . SCARNOX_API_KEY,
'Content-Type: application/json',
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 185, // token तैयार होने पर call लौटती है
]);
$raw = curl_exec($ch);
if ($raw === false) {
$error = curl_error($ch);
throw new RuntimeException('cURL error: ' . $error);
}
$data = json_decode((string) $raw, true);
if (!is_array($data)) {
throw new RuntimeException('Response was not JSON');
}
if (($data['errorId'] ?? 1) !== 0) {
throw new RuntimeException(
($data['errorCode'] ?? 'ERROR') . ': ' . ($data['errorDescription'] ?? 'unknown')
);
}
return (string) $data['solution']['token'];
}
Authorization header में Bearer prefix इस्तेमाल होता है। Timeout 185 सेकंड है, क्योंकि API token तैयार होने तक रुकती है, और कोई polling step नहीं है।
अस्थायी failures को retry करें
विफल solve का चार्ज नहीं लगता, इसलिए कुछ retries सस्ते हैं। Call के चारों ओर loop लगाएँ, और हर विफलता के बाद थोड़ा ज़्यादा इंतज़ार करें:
function solveWithRetry(string $sitekey, string $pageUrl, int $attempts = 3): string
{
$lastError = null;
for ($i = 1; $i <= $attempts; $i++) {
try {
return solveCaptcha($sitekey, $pageUrl);
} catch (RuntimeException $e) {
$lastError = $e;
fwrite(STDERR, "Attempt $i failed: " . $e->getMessage() . PHP_EOL);
sleep($i);
}
}
throw $lastError;
}
पूरी error policy, जिसमें यह शामिल है कि किन failures पर रुकना है, captcha solver error handling में है।
Cookie jar के साथ token submit करें
कई sites captcha को उस session से जोड़ती हैं जिसने form लोड किया था। Cookie file cURL को GET और POST पर एक ही cookies भेजने देती है। नीचे का function token को page के form पर submit करता है:
function submitToken(string $pageUrl, string $token): string
{
$cookieFile = sys_get_temp_dir() . '/scarnox-cookies.txt';
// पहले GET करें, ताकि site session cookie सेट कर दे।
$ch = curl_init($pageUrl);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEFILE => $cookieFile,
CURLOPT_COOKIEJAR => $cookieFile,
CURLOPT_TIMEOUT => 60,
]);
curl_exec($ch);
// फिर form को POST करें, दोनों captcha fields में token के साथ।
$fields = http_build_query([
'email' => 'you@example.com',
'h-captcha-response' => $token,
'g-recaptcha-response' => $token,
]);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $fields,
CURLOPT_TIMEOUT => 60,
]);
$body = curl_exec($ch);
if ($body === false) {
throw new RuntimeException('Form submit failed: ' . curl_error($ch));
}
return (string) $body;
}
दो बातें मायने रखती हैं। GET solve से पहले चलता है, ताकि cookie मौजूद हो। और token solve के ठीक बाद जाता है, क्योंकि वह लगभग दो मिनट में expire होता है। Solve call को batch की शुरुआत में नहीं, इस function से ठीक पहले रखें।
Cookie file उसी process की हर call के बीच साझा होती है, इसलिए अगर आप कई sign-ups समानांतर चलाते हैं, तो हर account या session को अपनी अलग file दें। नहीं तो एक session दूसरे की cookies को overwrite कर सकता है, और दूसरा POST ऐसे कारणों से फ़ेल होगा जो captcha की समस्या जैसे दिखते हैं, पर होते नहीं।
सब कुछ एक साथ
$pageUrl = 'https://example.com/signup';
$token = solveWithRetry('YOUR-SITEKEY-UUID', $pageUrl);
$html = submitToken($pageUrl, $token);
echo strlen($html) . " bytes returned\n";
Command line से चलाएँ:
php signup.php
Invisible widgets
अगर widget invisible है, तो solveCaptcha($sitekey, $pageUrl, true) बुलाएँ। तब request के साथ is_invisible field भेजा जाता है। कुछ pages को token के साथ एक JavaScript callback चाहिए होता है, जिसे server-side script ट्रिगर नहीं कर सकती। ऐसे में browser tool इस्तेमाल करें। invisible captcha guide मामलों को कवर करती है।
समय और expiry
Token लगभग दो मिनट के लिए valid है। Solve और submit को पास-पास रखें। captcha token expiry बताती है कि expired token को reject हुए token से कैसे पहचानें।
ज़िम्मेदारी से scrape करें
अगर आपकी PHP script सार्वजनिक pages scrape करती है, तो शुरू करने से पहले robots.txt और site के terms जाँचें। Requests के बीच delay जोड़ें, और वह personal data न रखें जिसकी ज़रूरत नहीं। Captcha solve करने से यह नहीं बदलता कि आपको क्या इकट्ठा करने की अनुमति है। कानूनी पहलू के लिए क्या web scraping legal है देखें।
कीमत
आप हर सफल solve के लिए भुगतान करते हैं। विफल solves का चार्ज नहीं लगता। मौजूदा दरों के लिए pricing page देखें, और मुफ़्त ट्रायल क्रेडिट से शुरू करें, कार्ड की ज़रूरत नहीं।