【问题标题】:Get capctha image using PHP CURL request and post some data back使用 PHP CURL 请求获取验证码图像并返回一些数据
【发布时间】: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 上,但我不知道在哪里。

【问题讨论】:

    标签: php curl captcha


    【解决方案1】:

    这是因为当您点击 url https://telematici.agenziaentrate.gov.it/VerificaCF/captcha?type=i 时,如果您尝试通过点击上面的 url 获取验证码图像,您将始终下载新的/不同的验证码 [不是浏览器中显示的验证码],您需要下载第一次点击时页面中显示的相同验证码。您可以为此使用 selenium+headless 浏览器。

    你应该使用 selenium 之类的东西来触发页面并通过注入 js 下载验证码

    这是从网页获取验证码图片的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 发送到您的服务以解决问题。 这次不会改了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 2014-09-16
      • 2014-04-23
      • 2020-03-14
      • 2017-02-13
      相关资源
      最近更新 更多