【发布时间】:2012-05-27 07:03:26
【问题描述】:
我正在尝试在受验证码保护的页面上自动化登录进度。我正在使用 Death By Captcha 将图像转换为文本,它似乎运行良好。我正在使用 curl 加载登录页面,检索验证码图像 url,将其发送到 DBC,取回文本并使用验证码文本向登录页面提交 POST 请求。
我遇到的问题是,当我提交发布请求时验证码图像会发生变化。由于在通过浏览器重新加载/或错误提交表单时我没有得到相同的行为(我一遍又一遍地得到相同的图像),我假设问题与 cookie 或我的其他东西有关缺少与会话相关的内容。
这是我用来检索数据和提交表单的代码:
$ch = curl_init();
// Not sure that I need it, just make sure that the session doesn't change...
curl_setopt($ch, CURLOPT_COOKIESESSION, false);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// It seems that PHPSESSID cookie parameter might be the parameter that keep the image the same, but it didn't work. I even read it dynamically from the cookie file but it still didn't work
//curl_setopt($ch, CURLOPT_COOKIE, "PHPSESSID=2bp3nhkp3bgftfrr1rjekg03o2");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieName);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieName);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $loginUrl);
$result = curl_exec($ch);
// Resolve the captcha and append it to the post parameters
$captchaText = $this->resolveCaptcha($result);
$postData .= '&LoginForm%5BverifyCode%5D='.$captchaText;
// Resubmit the form with the updated form data
curl_setopt($ch, CURLOPT_REFERER, $loginUrl);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt ($ch, CURLOPT_POST, 1); //FIXED
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
当我打印最终结果时,我可以看到验证码文本已成功提交,但图像本身已更改...
我还附上了在标准 Firefox 会话中使用 Tamper 捕获的请求参数的屏幕截图(因此如果我遗漏了什么,有人可能会发现)。
PHP/curl 提交代码完全适用于非基于验证码的网站,因此 POST 参数提交似乎可以正常工作。
可能是我在这里遗漏了一些非常基本的东西,我们将不胜感激。
我也查看了这些帖子,但找不到我正在寻找的答案。
How CURL Login with Captcha and Session
How to retrieve captcha and save session with PHP cURL?
https://stackoverflow.com/questions/8633282/curl-to-download-a-captcha-and-submit-it
【问题讨论】:
-
当然,使用验证码是为了阻止你这样做......如果网站的作者希望你使用 curl 访问页面,他一开始就不会实现验证码
-
正因为如此,解决该问题取决于您。虽然是一个编程问题,但 SO 不是为了协助恶作剧。
-
谢谢大家。这仍然是一个关于如何在传递 cookie 时保持会话完好无损的简单问题。在这种特定情况下,我试图从多个来源检索附属信息。但是,如果您不愿意提供帮助,那么我尊重您的意见。
标签: php session cookies curl captcha