【发布时间】:2020-03-09 01:58:01
【问题描述】:
我需要获取验证码图像,将其显示在页面上,并将有关该验证码的一些请求发送回同一页面。
我尝试了一些保存 cookie 的代码,但这不起作用。
// 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;
}
好吧,关于获取图像并显示它的部分是有效的。但是发送已解决的验证码的部分不起作用。我从我的页面手动解决验证码:
但它表明验证码是错误的:
我知道我正在发送该请求,因为我看到我在验证码旁边发送的输入代码。我想问题出在 cookie 上,但我不知道在哪里。
【问题讨论】: