在进行file_get_contents请求时是否可以接收远程服务器设置的cookie ?
我需要php来做一个http请求,存储cookie,然后使用存储的cookie发出第二个http请求.
Ja͢*_*͢ck 28
有一个神奇的变量,叫做$http_response_header; 它是一个包含所有收到的标题的数组.要提取cookie,您必须过滤掉以头开头的标头Set-Cookie:.
file_get_contents('http://example.org');
$cookies = array();
foreach ($http_response_header as $hdr) {
if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
parse_str($matches[1], $tmp);
$cookies += $tmp;
}
}
print_r($cookies);
Run Code Online (Sandbox Code Playgroud)
一种等效但不太神奇的方法是使用stream_get_meta_data():
if (false !== ($f = fopen('http://www.example.org', 'r'))) {
$meta = stream_get_meta_data($f);
$headers = $meta['wrapper_data'];
$contents = stream_get_contents($f);
fclose($f);
}
// $headers now contains the same array as $http_response_header
Run Code Online (Sandbox Code Playgroud)
Rag*_*geZ 22
您应该cURL为此目的使用,cURL实现一个名为cookie jar的功能,该功能允许将cookie保存在文件中并将其重用于后续请求.
这里有一个快速的代码snipet如何做到这一点:
/* STEP 1. let’s create a cookie file */
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
/* STEP 2. visit the homepage to set the cookie properly */
$ch = curl_init ("http://somedomain.com/");
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
/* STEP 3. visit cookiepage.php */
$ch = curl_init ("http://somedomain.com/cookiepage.php");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
Run Code Online (Sandbox Code Playgroud)
注意:必须注意你应该安装pecl扩展(或用PHP编译),否则你将无法访问cURL API.
Lae*_*eom 16
我意识到这是迟到的,但实际上有一种方法可以至少接收服务器发送的个别cookie.
I'm assuming you know how to do the whole stream_create_context business to get your file_get_contents http request rolling, and you just need assistance actually setting the cookies.
After running file_get_contents on a url, the (unfortunately, non-associative) array $http_response_header is set.
If the server is sending back a cookie, one of them will start with 'Set-Cookie: ', which you can extract with substr.
However, at the moment, it appears to me that one can only access -one- Set-Cookie through this variable, which is a limitation I am currently trying to find a way to work around.
继Laereom的回答之后,这里是如何获得多个cookie:
$cookies=array();
foreach($http_response_header as $s){
if(preg_match('|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|',$s,$parts))
$cookies[$parts[1]]=$parts[2];
}
Run Code Online (Sandbox Code Playgroud)
笔记:
explode(';',$parts[3])再用另一个循环来处理它(因为我不确定这些属性是否有固定的顺序.