1

I have below curl code to GET.

<?php  
function get_content($URL){
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
          curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
          curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); 
          curl_setopt($ch, CURLOPT_URL, $URL);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
      }
 echo get_content("http://www.domain.com/cool.php");
?>

I have used http headers and cookie looks like below

xidseq:22
xid:b05f251c-8a72-4c2b-a230-e03b9c5c87b7&&BAYIDSAMFE1C013&343
data:dsfsfssdss

I need to send GET request to http://www.domain.com/cool.php with some cookies.

how do i put the cookie in cookie.txt ?? is there any specific format for cookies..or just posting it works ?

Neenu Jino
  • 17
  • 1
  • 3
  • the cookie file should look like the old netscape-style cookies.txt – Marc B Oct 23 '12 at 19:21
  • dupe: http://stackoverflow.com/questions/12752555/sessions-with-curl and http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php <- has code example, gimme an up vote on one of those :) – ficuscr Oct 23 '12 at 19:23
  • its irrelevant to my question – Neenu Jino Oct 23 '12 at 19:24
  • I was wrong with my previous comment -- you will most likely want both. [Here is the best documentation](http://curl.haxx.se/docs/http-cookies.html) I could find at the time. – jedwards Oct 23 '12 at 19:27
  • I understand now. Thing Marc B. was on right track then. See RFC2965 – ficuscr Oct 23 '12 at 19:27
  • Also, [this mailing list thread](http://curl.haxx.se/mail/archive-2005-03/0099.html) might help. – jedwards Oct 23 '12 at 19:29
  • Possible duplicate - http://stackoverflow.com/questions/12885538/php-curl-and-cookies – ChrisF Feb 21 '14 at 19:40

1 Answers1

1

If script above doesn't work try:

curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt'); /* make sure you provide FULL PATH to cookie files*/

Check file permissions and ownership on dirname(__FILE__) . '/cookie.txt' . Make sure file is writable.

To put the cookie in cookie.txt you need to visit certain web page that contains them - only server side cookies you can fetch with cURL, javascript cookies is not reachable vie cURL, at least not directly.

If you want to create cookies manually for your GET request, read about Netscape cookies format or - best way, save some 'real website' cookies via CURLOPT_COOKIEJAR to see and understand format.

Miloš Đakonović
  • 3,751
  • 5
  • 35
  • 55