使用file_get_contents获取页面内容后,我们可以使用$http_response_header获取响应头信息,响应头信息里面一般包括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;
    }
}
/*  http://www.manongjc.com/article/1429.html */
print_r($cookies);

例外一种方法可以使用stream_get_ _data()实现:

if (false !== ($f = fopen(\'http://www.example.org\', \'r\'))) {
        $  = stream_get_ _data($f);
        $headers = $ [\'wrapper_data\'];

        $contents = stream_get_contents($f);
        fclose($f);
}
// $headers now contains the same array as $http_response_header
收藏 打印