0

I am authenticating towards an API endpoint which returns two Set-Cookie headers.

PHP Curl overwrites one of the two Set-Cookie headers and only returns one.

How can I work around this problem? This looks like a limitation in curl.

FileInputStream
  • 126
  • 2
  • 11
  • How are you retrieving the cookies? The code in [this question](https://stackoverflow.com/questions/895786/how-to-get-the-cookies-from-a-php-curl-into-a-variable) shows how to get multiple `Set-Cookie` headers. – Barmar Mar 03 '23 at 22:35

1 Answers1

0

What do you get in return?

For example, response script set two cookies:

<?php

http_response_code(200);

setcookie("CookName1", 1, time() + 60); // Set one;
setcookie("CookName2", 2, time() + 60); // Set two;

echo "All done";

Request script is:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http(s)://URL here");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
echo $result . PHP_EOL;

if (preg_match_all("/^Set-Cookie:\s*([^;]*)/umi", $result, $matches)) {
    var_dump($matches[1]);
}

Output:

HTTP/1.1 200 OK
Server: ....
Date: ......
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: CookName1=1; expires=Fri, XX-Xxx-2023 HH:MM:SS GMT; Max-Age=60
Set-Cookie: CookName2=2; expires=Fri, XX-Xxx-2023 HH:MM:SS GMT; Max-Age=60

All done
array(2) {
  [0]=>
  string(11) "CookName1=1"
  [1]=>
  string(11) "CookName2=2"
}
Dimitry
  • 128
  • 1
  • 1