file_get_contents接收cookie

Lou*_*s W 21 php cookies

在进行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.


Dar*_*ook 8

继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)

笔记:

  1. 我对正则表达式很自由; 如果你想更精确地研究RFC(即拒绝错误形成的cookie数据)
  2. 你会在$ parts [3]中找到path =,expires =等.我建议explode(';',$parts[3])再用另一个循环来处理它(因为我不确定这些属性是否有固定的顺序.
  3. 如果两个cookie具有相同的名称部分,则只有最后一个存活,这似乎是正确的.(我在我当前的项目中碰巧遇到这种情况;我认为这是网站上的一个错误,我正在进行屏幕抓取.)