在 PHP 中使用 curl 绕过验证码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10674078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bypassing Captcha with curl in PHP
提问by sagibb
I am trying to automate the login progress on a captcha protected page. I am using Death By Captcha to translate the image into text and it seems to be working well. I am using curl to load the login page, retrieve the captcha image url, send it to DBC, get the text back and submit a POST request to the login page with the captcha text.
我正在尝试在受验证码保护的页面上自动执行登录进度。我正在使用 Death By Captcha 将图像翻译成文本,它似乎运行良好。我正在使用 curl 加载登录页面,检索验证码图像 url,将其发送到 DBC,取回文本并使用验证码文本向登录页面提交 POST 请求。
The problem that I'm having is that the captcha image changes when I submit the post request. Since I do not get the same behavior when reloading/or wrongly submitting the form through a browser (I get the same image over and over again), I am assuming that the problem has to do with the cookies or something else that I'm missing that relates to the session.
我遇到的问题是,当我提交发布请求时,验证码图像会发生变化。由于在通过浏览器重新加载/或错误地提交表单时我没有得到相同的行为(我一遍又一遍地得到相同的图像),我假设问题与 cookie 或我正在做的其他事情有关缺少与会话相关的内容。
This is the code that I use to retrieve the data and submit the form:
这是我用来检索数据并提交表单的代码:
$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);
When I print the end result, I can see that the captcha text was submitted successfully but that the image itself has changed...
当我打印最终结果时,我可以看到验证码文本已成功提交,但图像本身已更改...
I am also attaching a screenshot of the request params as captured with Tamper in a standard Firefox session (so someone might spot if I'm missing something).
我还附上了在标准 Firefox 会话中使用 Tamper 捕获的请求参数的屏幕截图(因此如果我遗漏了什么,有人可能会发现)。


The PHP/curl submit code is fully working for non-captcha based sites so the POST parameters submission seems to be working.
PHP/curl 提交代码完全适用于基于非验证码的站点,因此 POST 参数提交似乎有效。
It could be that I'm missing something very basic here, any help will be much appreciated.
可能是我在这里遗漏了一些非常基本的东西,任何帮助将不胜感激。
I also took a look at these posts but couldn't find the answer that I'm looking for.
我也看了这些帖子,但找不到我正在寻找的答案。
How CURL Login with Captcha and Session
CURL 如何使用 Captcha 和 Session 登录
How to retrieve captcha and save session with PHP cURL?
https://stackoverflow.com/questions/8633282/curl-to-download-a-captcha-and-submit-it
https://stackoverflow.com/questions/8633282/curl-to-download-a-captcha-and-submit-it
回答by heximal
you're using
你正在使用
curl_setopt ($ch, CURLOPT_POST, 0);
in second curl_exec. shoudn't it be
在第二个 curl_exec 中。不应该
curl_setopt ($ch, CURLOPT_POST, 1);
?
?

