【问题标题】:CURLOPT_COOKIEFILE not sending cookie parameterCURLOPT_COOKIEFILE 不发送 cookie 参数
【发布时间】:2013-11-12 03:06:56
【问题描述】:

我在使用 PHP 的 cURL 库请求带有 cookie 的页面时遇到问题。代码如下:

$ch = curl_init();
$options = array(
    CURLOPT_URL => 'https://website.com/members',
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_COOKIEFILE => dirname(__FILE__) . '/../../cookie.txt'
);
curl_setopt_array($ch, $options);
$response = json_decode(curl_exec($ch));
curl_close($ch);

dirname(__FILE__) . '/../../cookie.txt' 指向一个有效的 cookie 文件,正如我通过使用 PHP 转储该文件的内容来验证的那样。所以 PHP 确实对该文件具有读取权限(并执行,我将该文件设置为 chmod 0777)。当我转储请求时,我看到:

* About to connect() to website.com port 80 (#0)
*   Trying 88.19.264.3... * connected
> GET /members HTTP/1.1
Accept: */*
Cookie: __utmz=1.1383258121.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=1; __utmb=1.8.10.1383258121; __utma=1.259269816.1383258121.1383258121.1383258121.1; sync=1; lang=en; https=1
Host: website.com

< HTTP/1.1 200 OK
< Server: nginx
< Date: Thu, 31 Oct 2013 22:36:07 GMT
< Content-Type: text/plain
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=8
< Vary: Accept-Encoding
< Access-Control-Allow-Origin: *
<
* Connection #0 to host website.com left intact
* Closing connection #0

cookie 标头使用cookie.txt 的内容设置,但是缺少验证会话所需的auth 参数。但是auth 参数肯定存在于cookie.txt 文件中。

我只是不明白为什么 cURL 只会发送 cookie.txt 文件中的一些参数,而不是 auth 参数。我试图删除所有不需要的参数,只留下auth,但它仍然没有被发送。

作为参考,auth 参数如下所示:

.website.com        TRUE    /       TRUE    1385850138.917861       auth    y2a4232344b4x2h403a423fgw2c443g4u2c49hg48494g4a4q2o5g4g4v274b4i4a4k5e4e4y2a4c4g4s2l56323b4d453s5946453b4t2c4y7d47336

我在命令行上使用 cURL 转储了 cookie 文件,因此我知道它的格式正确。

【问题讨论】:

    标签: php authentication cookies curl


    【解决方案1】:

    4.参数TRUE表示cookie只会通过安全连接发送。

    Cookie 文件格式:http://curl.haxx.se/mail/archive-2005-03/0099.html

    关于安全 cookie:http://en.wikipedia.org/wiki/HTTP_cookie#Secure_cookie

    【讨论】:

    • cURL 请求正在请求 https 页面。如果不能实现安全连接,会不会报错?
    • 你确定吗?它连接到端口 80,这意味着普通的 http。
    • 啊,好点子。当 cURL 无法建立 https 连接时,是否有机会静默连接到 http 而不是 https?我会检查我是否在 PHP 中支持 SSL。
    • 也许这有帮助:curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2012-04-14
    • 2013-09-24
    • 2016-12-23
    • 2010-09-30
    • 2018-05-23
    • 2011-10-19
    • 2020-03-13
    相关资源
    最近更新 更多