I need to get captcha image, show it on the page, and send some request about that captcha back to the same page.
I tried some code with saving cookies, but that does not work.
// 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;
}
Well, the part about getting an image and showing it works. But the part with sending solved captcha back is not working. I manually solve captcha from my page:
but it showing that captcha is wrong:
I know that I am sending that request because I see input code that I am sending beside captcha. I guess the problem is with cookies but I do not know where.