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

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 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示
  • ¥100 python怎么连接wxSQLite3加密的数据库
  • ¥20 创建taro项目,在vscode上能够写跨平台写微信小程序代码
  • ¥15 网站出现错误跳转问题
  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错