问题描述
我想了解一下cURL for cookie的四个选项:
I would like to understand the four options of cURL for cookies:
CURLOPT_COOKIESESSION
CURLOPT_COOKIEFILE
CURLOPT_COOKIEJAR
CURLOPT_COOKIE
我知道 COOKIEJAR
用于编写 cookie,而 COOKIEFILE
用于读取.那么什么是 COOKIESESSION
呢?CURLOPT_COOKIE
是自定义的,但我可以用它来保持与服务器的会话吗?
I understand that COOKIEJAR
is meant for writing cookies, and COOKIEFILE
is meant for reading.So what is COOKIESESSION
for ?The CURLOPT_COOKIE
is custom, but can I use it to maintain a session with the server ?
推荐答案
要理解 CURLOPT_COOKIESESSION
,您需要了解一些关于 cookie 的知识.Cookie 的有效期由发布 Cookie 的网站设置.如果 cookie 的过期日期已过,浏览器/客户端将不会发送它,并且它会被客户端删除.如果 cookie 设置为没有到期日期,则浏览器应使用该 cookie,直到浏览器会话关闭,或者用户注销并取消设置 cookie.
To understand CURLOPT_COOKIESESSION
, you need to know a couple of things about cookies. Cookies have expiration dates that are set by the website that issues the cookie. If an expiration date of a cookie has passed, the browser/client will not send it, and it will be deleted by the client. If a cookie is set with NO expiration date, the browser should use that cookie until the browser session is closed, or the user logs out and the cookie gets unset.
也就是说,CURLOPT_COOKIESESSION
是一种让 cURL 模拟关闭浏览器的方法.如果 COOKIEFILE
中有一些会话 cookie(没有过期的 cookie),如果它们存在于文件中,它通常会发送这些.如果您设置了 CURLOPT_COOKIESESSION
,则它不会发送任何没有过期日期的 cookie.
That said, CURLOPT_COOKIESESSION
is a way to get cURL to simulate having closed the browser. If the COOKIEFILE
has some session cookies in it (cookies with no expiration), it will normally send these if they were present in the file. If you set CURLOPT_COOKIESESSION
, then it will NOT send any of the cookies that have no expiration date.
CURLOPT_COOKIE
只是为您提供了一种设置将以原始格式发送到服务器的 cookie 数据的方法.例如,如果您有一个想要发送的原始 HTTP cookie,这将非常有用.如果没有此选项,您必须将这些 cookie 放入 COOKIEFILE
,或使用您拥有的原始值设置自定义 HTTP 标头 Cookie:
.
CURLOPT_COOKIE
just gives you a means of setting the cookie data that will be sent to the server in raw format. This is useful if for example you have a raw HTTP cookie that you would like to send. Without this option, you would have to get those cookies into the COOKIEFILE
, or set a custom HTTP header Cookie:
with the raw value you had.
这篇关于cURL -- cookie 和会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!