【问题标题】:CURL - While Loop until Captcha is sucesss?CURL - While 循环直到验证码成功?
【发布时间】:2012-12-19 03:10:12
【问题描述】:

假设验证码无效,需要重新下载新的验证码图片并重新验证验证码。怎么可能?

我有包含简短的例子,这是这样做的吗?

while (1) {
    $postData = http_build_query($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\**********************.crt");
    curl_setopt($ch, CURLOPT_URL, "https://domain.com/test" . $form_link);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiesPath . "/cookiefile.txt");
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiesPath . "/cookiefile.txt");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $page = curl_exec($ch);

    //Just a quick example
    if ($page == "Sucess") {
       break;
    } else {
        $ch = curl_init();
        //Some curl code here to Re-download Captcha Image (new image)
        $data['captchaText'] = CaptchaToText::Scan("images/captcha.jpg");
    }
}

【问题讨论】:

  • 与垃圾邮件无关!
  • 为什么 else 子句中有第二个 curl_init ?卷曲句柄可以重复使用,因此无需每次迭代都初始化一个新的,更不用说两个了,其中一个会立即被第二个丢弃。
  • 根据定义,您不能自动解决验证码 - 否则,它不会是 Completely Automated Public Turing test to tell Computers and Humans Apart
  • @Marc B,好点.. 所以我走在正确的道路上?如果验证码无效,则将一次又一次循环,直到$page == "Sucess"
  • @phihag 定义需要更新。验证码已经被破坏了很长时间(更不用说呼叫中心有大量的服务让很多人通过这些 API 填写它们)。

标签: php curl captcha


【解决方案1】:

是的,你做得对。但仅限于第一部分)

您已经启动了 cURL 资源 ($ch)。

所以你只需要通过 curl_exec($ch) 再次执行 cURL 请求,你就会得到一个新页面。 curl_setopt() 设置的所有 cURL 选项都保存在资源中。

代码如下:

if ($page == "Sucess") {
   break;
} else {
    $page = curl_exec($ch);
    //Some curl code here to Re-download Captcha Image (new image)
    $data['captchaText'] = CaptchaToText::Scan("images/captcha.jpg");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-12
    • 2018-11-23
    • 2014-12-08
    • 2020-09-29
    • 2021-05-13
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多