小白求教怎么抓取Cookies

2025-05-13 14:00:06
推荐回答(1个)
回答1:

cookies 不过是 request header 中的一项内容罢了,请求页面的时候把之前保存的 cookies 一起发过去,没什麼大不了的。
用 file_get_contents 也行,示例代码如下
// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);
?>

然後 $http_response_header 里有返回的新 cookies,保存下来供以後调用就行了。
抓取页面跟代理没什麼两样,只不过目标呈现不同罢了。