Why am I sometimes not getting a response with 2captcha API?

DJIGIT95

New member
I am trying to solve recaptcha V2 with API 2captcha ,

I am using this code:

Code:
<?php

function token(){

    $apiKey = "MY_API_KEY";

    $googleKey = "6LfBixYUAAAAABhdHynFUIMA_sa4s-XsJvnjtgB0";

    $pageUrl = "https://example.com/";

    $time = time();

    while ( true ) {

       $retrieve= file_get_contents("http://2captcha.com/in.php?key=".$apiKey."&method=userrecaptcha&googlekey=".$googleKey."&pageurl=".$pageUrl, false, $ctx);

       $first = array($retrieve);

       $result = explode('OK|',$first[0]);

       $hello = $result[1];

       $con="http://2captcha.com/res.php?key=".$apiKey."&action=get&id=".$hello;



       sleep(23);

       $getting = file_get_contents($con);

       $second = array($getting);

       $secondresult = explode('OK|',$second[0]);

       $reponsetoken = $secondresult[1];

       echo'<br/>';

       echo'<br/>';

       echo'get new captcha token ...';

       echo'<br/>';

       echo'<br/>';

       if ((time() - $time) >= 99) {

          echo date("Y:m:d g:i:s"), PHP_EOL;

          $time = time();

       }

       sleep(2);

    }

}



if (!empty($reponsetoken)) {

    file_put_contents( 'token.txt', $reponsetoken );

}  else{token();}

?>

Why am I sometimes not getting a response?

I am trying to make condition with a timeout here.

$retrieve= file_get_contents("http://2captcha.com/in.php?key=".$apiKey."&method=userrecaptcha&googlekey=".$googleKey."&pageurl=".$pageUrl, false, $ctx);

Then I want to loop all the code every 2 min 30 secs.

How can I use condition with file_get_contents()?

How to do loop the code every 2 min 30 secs?
 

Serj83

New member
This is my solution ,

2captcha take about 5 second and 100 second to solve captcha's .

In my last code the error was in sleep(23);

Code:
<?php
echo 'Starting Get Token....<br/>';
echo date("Y:m:d g:i:s");
$apiKey = "MY API KEY";
$googleKey = "6LfBixYUAAAAABhdHynFUIMA_sa4s-XsJvnjtgB0";
$pageUrl = "https://example.com";
$time = time();
while ( true ) {
    $ctx=stream_context_create(array('http'=>
    array(
        'timeout' => 20 // 30 sec
    )
    ));

    $retrieve= file_get_contents("http://2captcha.com/in.php?key=".$apiKey."&method=userrecaptcha&googlekey=".$googleKey."&pageurl=".$pageUrl, FALSE,$ctx);
    var_dump($retrieve);
    if (empty($retrieve))
    {
       $retrieve= file_get_contents("http://2captcha.com/in.php?key=".$apiKey."&method=userrecaptcha&googlekey=".$googleKey."&pageurl=".$pageUrl, FALSE,$ctx);
    }
    $first = array($retrieve);
    $result = explode('OK|',$first[0]);
    $hello = $result[1];
    $con="http://2captcha.com/res.php?key=".$apiKey."&action=get&id=".$hello;

    sleep(107);
    $getting = file_get_contents($con);
    $second = array($getting);
    $secondresult = explode('OK|',$second[0]);
    $x = $secondresult[1];
    echo $x;
    echo'<br/>';
    echo'<br/>';
    if (!empty($x)) {
       echo 'Task Finished ... <br/>';
       echo date("Y:m:d g:i:s");
       file_put_contents( 'token.txt', $x );
       sleep(120);
    }
}

?>