【问题标题】:Not able to read cookies file that saved by CURL无法读取 CURL 保存的 cookie 文件
【发布时间】:2018-12-26 02:51:33
【问题描述】:

我无法读取 CURL 保存的 cookie 文件

<?php

$path = dirname(__FILE__)."/cookies.txt";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.youtube.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $path);
curl_exec($ch);

$filecontent = file_get_contents($path);

var_dump($filecontent);

?>

这段代码应该向youtube发出curl请求,将cookies保存到cookies.txt文件中,然后使用file_get_contents读取它,cookies文件被保存但file_get_contents无法读取它,请问这是为什么?以及如何解决?

谢谢。

【问题讨论】:

    标签: php curl cookies file-get-contents


    【解决方案1】:

    在读取cookies.txt文件之前需要关闭curl资源

    试试这个

    <?php
    
    $path = dirname(__FILE__)."/cookies.txt";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://www.youtube.com/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $path);
    curl_exec($ch);
    
    // close cURL resource, and free up system resources
    curl_close($ch);
    
    $filecontent = file_get_contents($path);
    
    var_dump($filecontent);
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 2016-09-20
      • 2017-10-29
      • 1970-01-01
      • 2016-10-21
      • 2011-10-09
      • 1970-01-01
      相关资源
      最近更新 更多