curl没有cookie设置,没有cookie保存

时间:2014-09-12 22:24:48

标签: php macos curl cookies

我正在运行curl来测试cookie

curl -v --cookie cookies.txt -b cookie_to_del http://localhost/

没有cookie_to_del文件出现,但在输出中我看到:

* Added cookie TestCookie="my+cookie+value" for domain localhost, path /, expire 0
< Set-Cookie: TestCookie=my+cookie+value

同样的方法,在cookies.txt中我有几个cookie而且看不到它们的设置。我的服务器有php命令print_r($_COOKIE);,我显示空数组。

怎么了?

1 个答案:

答案 0 :(得分:2)

--cookie-b是等效的,它们只是指定要从中读取Cookie的文件。它不会创建或覆盖此文件。如果你多次给他们,只使用最后一次。

要保存Cookie,您必须使用--cookie-jar-c。所以它应该是:

curl -v --cookie cookies.txt --cookie-jar cookie_to_del http://localhost

如果您想使用发回的Cookie更新原始Cookie,则可以在两个otions中使用相同的文件。

My `cookies.txt` file contains:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.bridgebase.com TRUE    /   FALSE   0   SRV www3

我做了:

$ curl -s -v --cookie cookies.txt --cookie-jar cookies_to_del http://www.bridgebase.com/vugraph/schedule.php >/dev/null
* About to connect() to www.bridgebase.com port 80 (#0)
*   Trying 65.254.56.174... connected
* Connected to www.bridgebase.com (65.254.56.174) port 80 (#0)
> GET /vugraph/schedule.php HTTP/1.1
> User-Agent: curl/7.21.4 (x86_64-apple-darwin10.8.0) libcurl/7.21.4 OpenSSL/0.9.8y zlib/1.2.3 libidn/1.20
> Host: www.bridgebase.com
> Accept: */*
> Cookie: SRV=www3
> 
< HTTP/1.1 200 OK
< Server: nginx/1.6.0
< Date: Fri, 12 Sep 2014 22:55:29 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: close
< Vary: Accept-Encoding
< X-Powered-By: PHP/5.4.26-0
* Added cookie PHPSESSID="gu1fj84buirg370lee356b5r26" for domain www.bridgebase.com, path /, expire 0
< Set-Cookie: PHPSESSID=gu1fj84buirg370lee356b5r26; path=/; HttpOnly
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Pragma: no-cache
< Cache-control: private
< 
{ [data not shown]
* Closing connection #0

如您所见,它发送了标题Cookie: SRV=www3,它从文件中读取。生成的cookies_to_del文件包含:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

.bridgebase.com TRUE    /   FALSE   0   SRV www3
#HttpOnly_www.bridgebase.com    FALSE   /   FALSE   0   PHPSESSID   gu1fj84buirg370lee356b5r26

关于文件的格式,文档说:

  

从中读取cookie的文件的文件格式应该是纯HTTP标头或Netscape / Mozilla cookie文件格式。

我使用的文件是Netscape cookie文件。