Neenu Jino
Neenu Jino

Reputation: 17

Php : sending cookie in curl Request

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 ?

Upvotes: 1

Views: 7501

Answers (1)

Miloš Đakonović
Miloš Đakonović

Reputation: 3871

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.

Upvotes: 1

Related Questions