0

I'm not able to read cookies file that saved by 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);

$filecontent = file_get_contents($path);

var_dump($filecontent);

?>

This code supposed to make curl request to youtube, save the cookies to the cookies.txt file, then read it by using file_get_contents, the cookies file is saved but file_get_contents not able to read it, why is that please? and how to solve it?

Thank you.

user2203703
  • 1,955
  • 4
  • 22
  • 36

1 Answers1

1

You need to close the curl resource before reading the cookies.txt file

try this

<?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);

?>
Nuwan Attanayake
  • 1,161
  • 9
  • 20