【问题标题】:Multiple set-cookies in header rewrites each other curl标头中的多个 set-cookies 相互重写 curl
【发布时间】:2020-12-30 20:34:10
【问题描述】:

如果响应头包含两个Set-Cookie 值,如示例中所示:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=***; path=/; HttpOnly
X-Powered-By: ASP.NET
Date: Sat, 12 Sep 2020 15:26:07 GMT
Content-Length: 14713
Set-Cookie: BIGipServerauto-ext-vla216-pool=***; path=/; Httponly

,然后带参数

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);

cUrl 将只写入最后一个文件,因为覆盖。

有没有更好的方法,然后手动读取标题并通过file_put_contents 写入文件,以便为这种情况存储多个 cookie?

【问题讨论】:

  • CURLOPT_COOKIEFILE 是 curl 读取的地方。 CURLOPT_COOKIEJAR 是文件 curl writes cookies to
  • @DanielStenberg 那又怎样?
  • 所以我不明白这个问题。
  • @DanielStenberg 我需要存储来自标头的两个 cookie 怎么做?
  • CURLOPT_COOKIEJAR 这样做

标签: php curl cookies


【解决方案1】:

Curl 接受来自不同 Set-Cookie 标头的 cookie。

查看文档:

https://curl.haxx.se/libcurl/c/CURLOPT_COOKIEFILE.html

CURLOPT_COOKIEFILE - 从中​​读取 cookie 的文件名

https://curl.haxx.se/libcurl/c/CURLOPT_COOKIEJAR.html

CURLOPT_COOKIEJAR - 存储 cookie 的文件名

【讨论】:

  • Curl 确实接受来自不同 Set-Cookie 标头的 cookie。 在您提到的文档中没有找到任何相关内容
  • @Artur 文档仅告知 cookiejar 是唯一应该写入的文件。 Curl 接受多个 Set-Cookie 标头,因为这是默认行为。我无法重现它无法做到这一点的任何示例。
  • 为什么要记录下来?它记录了您启用 cookie 引擎,然后它将理解和使用 cookie。这当然包括处理大量它们。
猜你喜欢
  • 2012-06-13
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多