在PHP中使用cURL绕过验证码 [英] Bypassing Captcha with curl in PHP

查看:0
本文介绍了在PHP中使用cURL绕过验证码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在验证码保护的页面上自动执行登录过程。我正在使用Death by Captcha将图像转换为文本,它似乎运行得很好。我正在使用cURL加载登录页面、检索验证码图像URL、将其发送到DBC、取回文本并使用验证码文本向登录页面提交POST请求。

我遇到的问题是,当我提交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会话中通过篡改捕获的请求参数的屏幕截图(因此,如果我遗漏了什么,可能会有人发现)。

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中使用cURL绕过验证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆