php - 在 PHP 中使用 curl 绕过验证码

标签 php session cookies curl captcha

我正在尝试在受验证码保护的页面上自动执行登录进度。我正在使用 Death By Captcha 将图像翻译成文本,它似乎运行良好。我正在使用 curl 加载登录页面,检索验证码图像 url,将其发送到 DBC,取回文本并使用验证码文本向登录页面提交 POST 请求。

我遇到的问题是,当我提交发布请求时,验证码图像会发生变化。由于在通过浏览器重新加载/或错误地提交表单时我没有得到相同的行为(我一遍又一遍地得到相同的图像),我假设问题与 cookie 或我正在做的其他事情有关缺少与 session 相关的内容。

这是我用来检索数据并提交表单的代码:

$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 session 中使用 Tamper 捕获的请求参数的屏幕截图(因此如果我遗漏了什么,有人可能会发现)。

Browser request parameters

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_setopt ($ch, CURLOPT_POST, 0);

在第二个 curl_exec 中。不应该
curl_setopt ($ch, CURLOPT_POST, 1);

?

关于php - 在 PHP 中使用 curl 绕过验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10674078/

相关文章:

php - Mysql Where Column A = X and Column B = Y and or Column B = Z

php - 对二维数组使用神奇的 __set() 方法

php - 数据库设计: best practice for designing a forum table

php - 希伯来语字体未显示

perl - 我如何手动将 cookie 添加到 WWW::Mechanize 已经存在的 cookiejar

java - 在桌面应用程序中保存用户名和密码

php - 访问 magento 管理面板时连接被重置

PHP 用户 session 获取 'Muddled'

android - 应用程序生命周期,设置计时器以注销用户

java - JSESSIONID 后缀为 .undefined