1

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

I am using this 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?

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

2

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

<?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);
    }
}

?>
bcperth
  • 2,191
  • 1
  • 10
  • 16
  • @bcperth , i am very newbie in coding this why i am coding a horriblle code :) – Ben Ammar Amine Nov 10 '18 at 12:01
  • I fixed it up for you, But read this https://codehs.gitbooks.io/introcs/content/Programming-with-Karel/how-to-indent-your-code.html and this https://meta.stackoverflow.com/questions/300123/so-should-automatically-indent-code – bcperth Nov 10 '18 at 21:48