繁体   English   中英

为什么有时我无法通过2captcha API得到响应?

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

我正在尝试使用API 2captcha解决recaptcha V2

我正在使用此代码:

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

为什么有时我没有得到回应?

我试图在这里超时设置条件。

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

然后我想每2分钟30秒循环所有代码。

如何在file_get_contents()使用condition?

如何每2分钟30秒循环一次代码?

这是我的解决方案,

2captcha大约需要5秒和100秒才能解决captcha's

在我的上一个代码中,错误出在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);
    }
}

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM