我需要获得验证码图像,在页面上显示它,并发送一些关于验证码的请求回到同一页。
我尝试了一些保存cookies的代码,但不起作用。
// Referer value for login request
$indexreferer = "https://dichiarazioneprecompilata.agenziaentrate.gov.it/index.htm?v=20190502";
$captchareferer = "https://telematici.agenziaentrate.gov.it/VerificaCF/VerificaCf.do";
// Note that __DIR__ can also be used in PHP 5.3+
$cookieJar = dirname(__FILE__) . '/cookie.txt';
// The User-Agent string to send
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5";
$ch = curl_init('https://telematici.agenziaentrate.gov.it/VerificaCF/VerificaCf.do');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// Set header-related options
curl_setopt($ch, CURLOPT_REFERER, $indexreferer);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
$result = curl_exec($ch);
//getcaptcha($cookies['JSESSIONID']);
echo $cookieJar;
getcaptcha();
function getcaptcha()
{
global $cookieJar;
global $captchareferer;
global $userAgent;
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://telematici.agenziaentrate.gov.it/VerificaCF/captcha?type=i');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// Set header-related options
curl_setopt($ch, CURLOPT_REFERER, $captchareferer);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
file_put_contents('./logo.jpg',$result);
}
if(isset($_POST['submit']))
{
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://telematici.agenziaentrate.gov.it/VerificaCF/VerificaCf.do');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "cf=55555&inCaptchaChars=".$_POST['code']."");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
// Set header-related options
curl_setopt($ch, CURLOPT_REFERER, $captchareferer);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
echo $result;
}嗯,关于获取图像并显示它的工作的部分。但将已解决的验证码发回的部分不起作用。我从我的页面手动解决验证码:

但它表明验证码是错的:

我知道我正在发送该请求,因为我在captcha旁边看到了我正在发送的输入代码。我猜问题出在饼干上,但我不知道在哪里。
发布于 2020-03-09 01:58:01
这是因为当你点击url https://telematici.agenziaentrate.gov.it/VerificaCF/captcha?type=i时,你会得到新的验证码。如果你试图通过点击上面的url来获得验证码图像,你将总是下载新的/不同的验证码,而不是浏览器中显示的验证码,你需要下载第一次点击时在页面中显示的相同验证码。你可以使用selenium+headless浏览器来做这件事。
您应该使用selenium之类的东西来触发页面,并通过注入js来下载captcha。
这是js注入代码,用于从网页获取验证码图像。
var imageElement = document.getElementById("imgCaptcha"); //replace imgCaptcha with original captcha id
var canvasElement = document.createElement('canvas');
canvasElement.width = imageElement.naturalWidth;
canvasElement.height = imageElement.naturalHeight;
canvasElement.getContext('2d').drawImage(imageElement, 0, 0);
var base64ImageString = canvasElement.toDataURL('image/jpeg').split(',')[1];将此base64ImageString发送到您的服务以解决此问题。这一次它不会改变。
https://stackoverflow.com/questions/56329822
复制相似问题