CURL - 循环直到Captcha成功?

时间:2011-06-30 14:53:58

标签: php curl captcha

假设验证密钥无效,则需要再次下载新验证码图像并重新验证验证码密钥。怎么办?

我有简短的例子,这是怎么做的?

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

1 个答案:

答案 0 :(得分: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");
}