Jump to content

Need help using curl and loading cookie


insomnomaniac

Recommended Posts

Hi All,

 

I'm not too familiar with using curl and I'm stuck on a script I'm trying to write and I hope I can get some help.

 

Problem:

Need to fetch data from a page that requires an age verification cookie.

 

The page will redirect to an age verification page if it doesn't find a cookie.

 

Help:

What I'd like to do load the cookie (manually saved on the server as /cookie.txt) and load it when I try to fetch the data from the page. If this isn't the right way to do it, please advise.

 

Here's the code I've tried after some research but I haven't had any luck with it.

 

  $ch=curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');

  curl_setopt($ch, CURLOPT_MAXREDIRS, 2);

  curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);

  $data = curl_exec($ch);

  curl_close($ch);

 

$data contains the data from the age verification page

 

Any help/feedback would be greatly appreciated. Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/123896-need-help-using-curl-and-loading-cookie/
Share on other sites

Why cookies? Why no sessions? And using cURL? Are you talking to an external (from your website) API?

Perhaps just using the following:

<?php
$age = $_POST['age'];
if($age >= 18){
  setCookie("isOldEnough","1");
}
?>

On another page
[code]
<?php
echo $_COOKIE['isOldEnough'];
?>

 

The above assumes that you are a receiving an age input from a form in a previous page.

 

It really does depend on what you're attempting to achieve here...[/code]

Why cookies? Why no sessions? And using cURL? Are you talking to an external (from your website) API?

 

Thanks for the reply aschk.

 

I'm trying to grab contents from a website that does not offer APIs so I'd like to load their age verified cookie on my server (manually saved somewhere on my server) before accessing the website because it will redirect to the age verification page if that cookie does not exist so I can't grab the contents from the actual page I'm trying to access if I don't load the cookie.

 

Any ideas?

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.