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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 目前wxid加好友的渠道都被和谐了吗
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出