简体   繁体   English

是否可以使用CURL设置cookie内容?

[英]Is it possible to set the cookie content with CURL?

I have been searching for a way, to specify the cookie data for CURL. 我一直在寻找一种方法,为CURL指定cookie数据。 I have found some solutions on how to save the cookies from a visited page, but that's not what I need. 我找到了一些关于如何从访问过的页面保存cookie的解决方案,但这不是我需要的。 What I want is, to write the data for the cookie myself, so CURL uses it. 我想要的是,自己编写cookie的数据,所以CURL使用它。

You can use curl_setopt with the CURLOPT_COOKIE constant: 您可以将curl_setopt与CURLOPT_COOKIE常量一起使用:

<?php
// create a new cURL resource
$ch = curl_init();

// cookies to be sent
curl_setopt($ch, CURLOPT_COOKIE, "fruit=apple; colour=red");

You really should read the documentation - it's listed with exactly the keywords you'd expect and contains a lot of helpful info: 你真的应该阅读文档 - 它列出了你期望的关键字,并包含很多有用的信息:

-b, --cookie -b,--cookie

(HTTP) Pass the data to the HTTP server as a cookie. (HTTP)将数据作为cookie传递给HTTP服务器。 It is supposedly the data previously received from the server in a "Set-Cookie:" line. 据推测,它是先前从“Set-Cookie:”行中的服务器接收的数据。 The data should be in the format "NAME1=VALUE1; NAME2=VALUE2". 数据应采用“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. 如果行中没有使用'='符号,则将其视为用于读取先前存储的cookie行的文件名,如果它们匹配,则应在此会话中使用。 Using this method also activates the "cookie parser" which will make curl record incoming cookies too, which may be handy if you're using this in combination with the -L, --location option. 使用此方法还会激活“cookie解析器”,这将使curl记录传入的cookie,如果您将它与-L, - location选项结合使用,这可能很方便。 The file format of the file to read cookies from should be plain HTTP headers or the Netscape/Mozilla cookie file format. 从中读取cookie的文件的文件格式应该是纯HTTP标头或Netscape / Mozilla cookie文件格式。

NOTE that the file specified with -b, --cookie is only used as input. 请注意,使用-b,--cookie指定的文件仅用作输入。 No cookies will be stored in the file. 文件中不会存储任何cookie。 To store cookies, use the -c, --cookie-jar option or you could even save the HTTP headers to a file using -D, --dump-header! 要存储cookie,请使用-c, - cool-jar选项,或者甚至可以使用-D, - dump-header将HTTP标头保存到文件中!

If this option is set more than once, the last one will be the one that's used. 如果多次设置此选项,则最后一个选项将是使用的选项。

cURL can use a cookie file in Netscape format . cURL可以使用Netscape格式的cookie文件。 Just create such a file yourself and use as the CURLOPT_COOKIEFILE option. 只需自己创建这样一个文件并用作CURLOPT_COOKIEFILE选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM