duanjie3267 2017-01-16 17:56 采纳率: 0%
浏览 231
已采纳

PHP CURL从远程网页获取cookie

Im trying to take the cookie file from the webpage with PHP and CURL: https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaSituacao/ConsultaPublica.asp

But after the first time i can´t take it more, the cookie is avaliable in the "RESPONSE HEADER" just in the 1º connection, after the 1º connection it isn´t more.

I tried to make a CURL request cleaning the cache, or avoiding the cache, but i haven´t success.

So when i try to do it in the browser, what i noticed is that all the time i clean the cache i can see the cookie in the "RESPONSE HEADER", how can i do it using PHP + CURL, i need to get the cookie?

Thanks!

1º request after to clean cache browser 1º request after to clean cache browser

Next Access Next Access

CODE:

define('COOKIELOCAL', realpath('./').'/');

        $ch = curl_init("https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaPublica.asp");

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 3);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);

        /* trying to clean the cache */
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Pragma: no-cache",
            "Cache-control: no-cache"
        ));

        $result = curl_exec($ch);

        echo 'Cookie...........:' . file_get_contents($cookieFile); // Empty cookie file here until now
  • 写回答

1条回答 默认 最新

  • du7535 2017-01-16 19:14
    关注

    this should work

    <?php
    declare(strict_types=1);
    
    $ch=curl_init('https://www.receita.fazenda.gov.br/Aplicacoes/SSL/ATCTA/CPF/ConsultaSituacao/ConsultaPublica.asp');
    $cookiefile=tmpfile();
    $cookiefilepath=stream_get_meta_data($cookiefile)['uri'];
    curl_setopt_array($ch, array(
            CURLOPT_COOKIEFILE=>$cookiefilepath,
            CURLOPT_COOKIEJAR=>$cookiefilepath,
            CURLOPT_SSL_VERIFYPEER=>false,
            CURLOPT_SSL_VERIFYHOST=>false
    ));
    curl_exec($ch);
    curl_close($ch);
    $cookies=file_get_contents($cookiefilepath);
    var_dump($cookies);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出