How to allow cURL to automatically add recived cookies to the request?

3.2k views Asked by At

I am trying to communicate with a RESP API using cURL calls via PHP.

The first call to the API is to login by passing a username and a password. Once the API receive my request, it returns something like this in the headers

HTTP/1.1 201 Created
ININ-ICWS-CSRF-Token: WAhtYWxoabcfa1dBY2NvUkRJWCQ2Yzg5YefgOC01YTI0LTQ1MjEtYTdgdd1iMzAyNGRhZmRjZTBYCjEwLjAuNC4xNjA=
ININ-ICWS-Session-ID: 2562886002
Set-Cookie: icws_2562886002=1924Pe25-d47c-4d07-9546-9fcuijfdd0b02; Path=/icws/2562886002
Location: /icws/2562886002/connection
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Content-Type: application/vnd.inin.icws+JSON; charset=utf-8
Date: Thu, 14 May 2015 17:49:20 GMT
Server: HttpPluginHost
Content-Length: 238

Now, along with any additional call to the API, the cookie value that was returned in the header must be included in the new request. (in this case: icws_2562886002=1924Pe25-d47c-4d07-9546-9fcuijfdd0b02)

How can I configure my cURL call to automatically pass back the cookie that is received?

therefore, with every request I will see to have Cookie: icws_2562886002=1924Pe25-d47c-4d07-9546-9fcuijfdd0b02 in the header.

I know I can manually added it like this

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cookie: icws_2562886002=1924Pe25-d47c-4d07-9546-9fcuijfdd0b02'));

But there has to be a way for the cURL to automatically add the cookie value to the request.

I also tried to add this

curl_setopt($ch, CURLOPT_COOKIE, true); 

But did not work either

2

There are 2 answers

4
Axalix On BEST ANSWER
$cookiesFile = 'cookies.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiesFile); // write
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiesFile); // read
0
Misunderstood On

Often the cookies are set in a 302 redirect. This gets to be problematic with cURL and cookies. So much so I wrote my own cookie routines.

When there is a redirect I do not allow cURL to follow :

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

I get the cookies from the Header so I have to tell cURL to give it:

curl_setopt($ch, CURLOPT_HEADER, true);

After executing cURL

  $data = curl_exec($ch);

Get the Response Header:

  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $responseHeader= substr($data,0,$skip);
  $data =  substr($data,$skip);

Get all the cookies:

  $e = 0;
  while(true){
    $s = strpos($responseHeader,'Set-Cookie: ',$e);
    if (!$s){break;}
    $s += 12;
    $e = strpos($responseHeader,';',$s);
    $cookie = substr($responseHeader,$s,$e-$s) ;
    $s = strpos($cookie,'=');
    $key = substr($cookie,0,$s);
    $value = substr($cookie,$s);
    $cookies[$key] = $value;
  }

Reconstruct the cookies:

 $cookie = '';
 $show = '';
 $delim = '';
 foreach ($cookies as $k => $v){
   $cookie .= "$delim$k$v";
   $delim = '; ';
 }

Then use:

curl_setopt($ch, CURLOPT_COOKIE, $cookie );

Many use:

CURLOPT_COOKIEJAR
CURLOPT_COOKIEFILE

There are many times the Cookie Jar does not work. If you are doing something simple, they work fine.

When there is a redirect or you need the cookies in a subsequent request where the Cookie Jar crumbles.

Especially problematic when you need some other data from the redirected page. I have run in to case where there are a series of up to half a dozen redirects and the cookies constantly change.