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 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形