在两个 curl 请求之间保存 cookie
Save cookies between two curl requests
我知道使用 cURL
我可以看到我收到的 cookies / headers 使用
curl --head www.google.com
而且我知道我可以使用
将 headers 添加到我的请求中
curl --cookie "Key=Value" www.google.com
我目前正在测试一个需要持久性 cookie 的问题,而且可能有很多。
How can I efficiently preserve cookies between two cURL
requests?
如果可能,使用临时文件进行存储。
使用 --cookie-jar
或 --dump-header
参数将收到的 cookie 保存到文件中。 --cookie
参数稍后可以从该文件中读回 cookie。
-b, --cookie <name=data>
(HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously received from the server in a "Set-Cookie:" line. The data should be in the format "NAME1=VALUE1; NAME2=VALUE2".
If no '=' symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used in this session if they match. Using this method also activates the cookie engine which will make curl record incoming cookies too, which may be handy if you're using this in combination with the -L, --location option. The file format of the file to read cookies from should be plain HTTP headers (Set-Cookie style) or the Netscape/Mozilla cookie file format.
The file specified with -b, --cookie is only used as input. No cookies will be written to the file. To store cookies, use the -c, --cookie-jar option.
Exercise caution if you are using this option and multiple transfers may occur. If you use the NAME1=VALUE1; format, or in a file use the Set-Cookie format and don't specify a domain, then the cookie is sent for any domain (even after redirects are followed) and cannot be modified by a server-set cookie. If the cookie engine is enabled and a server sets a cookie of the same name then both will be sent on a future transfer to that server, likely not what you intended. To address these issues set a domain in Set-Cookie (doing that will include sub-domains) or use the Netscape format.
If this option is used several times, the last one will be used.
-c, --cookie-jar <file name>
(HTTP) Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies previously read from a specified file as well as all cookies received from remote server(s). If no cookies are known, no data will be written. The file will be written using the Netscape cookie file format. If you set the file name to a single dash, "-", the cookies will be written to stdout.
This command line option will activate the cookie engine that makes curl record and use cookies. Another way to activate it is to use the -b, --cookie option.
If the cookie jar can't be created or written to, the whole curl operation won't fail or even report an error clearly. Using -v will get a warning displayed, but that is the only visible feedback you get about this possibly lethal situation.
Since 7.43.0 cookies that were imported in the Set-Cookie format without a domain name are not exported by this option.
If this option is used several times, the last specified file name will be used.
-D, --dump-header <file>
Write the protocol headers to the specified file.
This option is handy to use when you want to store the headers that an HTTP site sends to you. Cookies from the headers could then be read in a second curl invocation by using the -b, --cookie option! The -c, --cookie-jar option is a better way to store cookies.
When used in FTP, the FTP server response lines are considered being "headers" and thus are saved there.
If this option is used several times, the last one will be used
或者,不使用命令行 cURL app, write some code that uses the libCurl library。这将使您更直接地控制 cookie 处理。 libCurl 有几个与 HTTP cookie 相关的特性:
curl_easy_getinfo()
的选项:
- CURLINFO_COOKIELIST - 获取所有已知的 cookie
curl_easy_setopt()
的选项:
CURLOPT_COOKIE - 设置 HTTP Cookie 头的内容
中读取 cookie 的文件名
CURLOPT_COOKIEJAR - 将 cookie 存储到的文件名
CURLOPT_COOKIESESSION - 开始新的 cookie 会话
CURLOPT_COOKIELIST - 添加或操作保存在内存中的 cookie
然后您可以根据需要存储 cookie,并根据需要将它们分配给以后的 HTTP 会话。
curl -b cookie.txt -c cookie.txt <url>
或 curl --cookie cookie.txt --cookie-jar cookie.txt <url>
将存储和发送已保存的 cookie。
我要感谢这里的每个人帮助我找到使用 CURL 登录我的 wordpress 站点的解决方案。这是我的一点小贡献,希望对以后遇到这个问题的人也有帮助
对于比 1706 更新的 windows 10 版本,您可以从命令行使用 curl。
curl -c c:\Users\<your-name>\Desktop\cookie.txt -F "log=<your username>" -F "pwd=<your-pwd>" https://acme.net/wp-login.php
您不会在终端中看到任何响应。它只是自行刷新,但您的 cookie 已存储。
然后在下面输入以读取和发送您刚刚存储的 cookie。
curl -b c:\Users\<your-name>\Desktop\cookie.txt -L https://acme.net/my-profile/
只有登录用户才能访问的整个网页将在您的终端内呈现,包括通过 javascript.
动态加载的任何数据
即使您的 WP 登录 url 的终点是 '/register/log-in' 或 '/login' 我仍然建议您使用 '/wp-login.php' 作为终点.这是因为某些 WP 主题在其登录页面的隐藏输入中有一个登录随机数。原生WP登录页面没有这个
您可以参考的其他来源:
https://wpmayor.com/login-to-wordpress-dashboard-via-curl/
https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
https://www.youtube.com/watch?v=B4ilccLUQVs
https://makandracards.com/makandra/48262-how-to-use-cookies-with-curl
只是为了扩展已接受的答案。使用 --cookie 和 --cookie-jar 标志可以在没有文件写入的情况下使用,使用进程替换:
- 写入 STDOUT 并保存到 $cookie 变量
cookie=$(curl -c - <url>)
- 从 $cookie 变量读取 cookie
curl --cookie <(echo "$cookie") <url>
我知道使用 cURL
我可以看到我收到的 cookies / headers 使用
curl --head www.google.com
而且我知道我可以使用
将 headers 添加到我的请求中curl --cookie "Key=Value" www.google.com
我目前正在测试一个需要持久性 cookie 的问题,而且可能有很多。
How can I efficiently preserve cookies between two
cURL
requests?
如果可能,使用临时文件进行存储。
使用 --cookie-jar
或 --dump-header
参数将收到的 cookie 保存到文件中。 --cookie
参数稍后可以从该文件中读回 cookie。
-b, --cookie <name=data>
(HTTP) Pass the data to the HTTP server as a cookie. It is supposedly the data previously received from the server in a "Set-Cookie:" line. The data should be in the format "NAME1=VALUE1; NAME2=VALUE2".
If no '=' symbol is used in the line, it is treated as a filename to use to read previously stored cookie lines from, which should be used in this session if they match. Using this method also activates the cookie engine which will make curl record incoming cookies too, which may be handy if you're using this in combination with the -L, --location option. The file format of the file to read cookies from should be plain HTTP headers (Set-Cookie style) or the Netscape/Mozilla cookie file format.
The file specified with -b, --cookie is only used as input. No cookies will be written to the file. To store cookies, use the -c, --cookie-jar option.
Exercise caution if you are using this option and multiple transfers may occur. If you use the NAME1=VALUE1; format, or in a file use the Set-Cookie format and don't specify a domain, then the cookie is sent for any domain (even after redirects are followed) and cannot be modified by a server-set cookie. If the cookie engine is enabled and a server sets a cookie of the same name then both will be sent on a future transfer to that server, likely not what you intended. To address these issues set a domain in Set-Cookie (doing that will include sub-domains) or use the Netscape format.
If this option is used several times, the last one will be used.
-c, --cookie-jar <file name>
(HTTP) Specify to which file you want curl to write all cookies after a completed operation. Curl writes all cookies previously read from a specified file as well as all cookies received from remote server(s). If no cookies are known, no data will be written. The file will be written using the Netscape cookie file format. If you set the file name to a single dash, "-", the cookies will be written to stdout.
This command line option will activate the cookie engine that makes curl record and use cookies. Another way to activate it is to use the -b, --cookie option.
If the cookie jar can't be created or written to, the whole curl operation won't fail or even report an error clearly. Using -v will get a warning displayed, but that is the only visible feedback you get about this possibly lethal situation.
Since 7.43.0 cookies that were imported in the Set-Cookie format without a domain name are not exported by this option.
If this option is used several times, the last specified file name will be used.
-D, --dump-header <file>
Write the protocol headers to the specified file.
This option is handy to use when you want to store the headers that an HTTP site sends to you. Cookies from the headers could then be read in a second curl invocation by using the -b, --cookie option! The -c, --cookie-jar option is a better way to store cookies.
When used in FTP, the FTP server response lines are considered being "headers" and thus are saved there.
If this option is used several times, the last one will be used
或者,不使用命令行 cURL app, write some code that uses the libCurl library。这将使您更直接地控制 cookie 处理。 libCurl 有几个与 HTTP cookie 相关的特性:
curl_easy_getinfo()
的选项:
- CURLINFO_COOKIELIST - 获取所有已知的 cookie
curl_easy_setopt()
的选项:
CURLOPT_COOKIE - 设置 HTTP Cookie 头的内容
- 中读取 cookie 的文件名
CURLOPT_COOKIEJAR - 将 cookie 存储到的文件名
CURLOPT_COOKIESESSION - 开始新的 cookie 会话
CURLOPT_COOKIELIST - 添加或操作保存在内存中的 cookie
然后您可以根据需要存储 cookie,并根据需要将它们分配给以后的 HTTP 会话。
curl -b cookie.txt -c cookie.txt <url>
或 curl --cookie cookie.txt --cookie-jar cookie.txt <url>
将存储和发送已保存的 cookie。
我要感谢这里的每个人帮助我找到使用 CURL 登录我的 wordpress 站点的解决方案。这是我的一点小贡献,希望对以后遇到这个问题的人也有帮助
对于比 1706 更新的 windows 10 版本,您可以从命令行使用 curl。
curl -c c:\Users\<your-name>\Desktop\cookie.txt -F "log=<your username>" -F "pwd=<your-pwd>" https://acme.net/wp-login.php
您不会在终端中看到任何响应。它只是自行刷新,但您的 cookie 已存储。
然后在下面输入以读取和发送您刚刚存储的 cookie。
curl -b c:\Users\<your-name>\Desktop\cookie.txt -L https://acme.net/my-profile/
只有登录用户才能访问的整个网页将在您的终端内呈现,包括通过 javascript.
动态加载的任何数据即使您的 WP 登录 url 的终点是 '/register/log-in' 或 '/login' 我仍然建议您使用 '/wp-login.php' 作为终点.这是因为某些 WP 主题在其登录页面的隐藏输入中有一个登录随机数。原生WP登录页面没有这个
您可以参考的其他来源:
https://wpmayor.com/login-to-wordpress-dashboard-via-curl/
https://gist.github.com/subfuzion/08c5d85437d5d4f00e58
https://www.youtube.com/watch?v=B4ilccLUQVs
https://makandracards.com/makandra/48262-how-to-use-cookies-with-curl
只是为了扩展已接受的答案。使用 --cookie 和 --cookie-jar 标志可以在没有文件写入的情况下使用,使用进程替换:
- 写入 STDOUT 并保存到 $cookie 变量
cookie=$(curl -c - <url>)
- 从 $cookie 变量读取 cookie
curl --cookie <(echo "$cookie") <url>