您还可以尝试curl,这是检索所有头的最短可能示例,如下所示:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://stackoverflow.com');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
function read_header($ch, $string) {
print "Received header: $string";
return strlen($string);
}
输出:
[~]> php headers.php
Received header: HTTP/1.1 200 OK
Received header: Cache-Control: private
Received header: Content-Type: text/html; charset=utf-8
Received header: Expires: Mon, 31 Aug 2009 09:38:45 GMT
Received header: Server: Microsoft-IIS/7.0
Received header: Date: Mon, 31 Aug 2009 09:38:45 GMT
Received header: Content-Length: 118666
Received header:
当然,它只是您想要的头,然后fsockopen也可以工作。除此之外,您应该使用head,因为您只需要头部,而不是内容。
此外,curl还为https url-s工作(前提是您已经用SSL支持编译了它)。