drdawt9210 2019-05-27 16:48
浏览 162

使用PHP CURL请求获取验证码图像并发回一些数据

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:

enter image description here

but it showing that captcha is wrong:

enter image description here

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能
    • ¥15 jmeter脚本回放有的是对的有的是错的
    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型