在PHP中使用HTTP的HEAD命令最简单的方法是什么? [英] What is the easiest way to use the HEAD command of HTTP in PHP?
问题描述
我想将超文本传输协议的HEAD命令发送到PHP中的服务器以检索标头,但不是内容或URL。如何以有效的方式执行此操作?
I would like to send the HEAD command of the Hypertext Transfer Protocol to a server in PHP to retrieve the header, but not the content or a URL. How do I do this in an efficient way?
可能最常见的用例是检查死网页链接。为此,我只需要HTTP请求的回复代码而不是页面内容。
使用 file_get_contents(http:// ...)
可以轻松地使用PHP获取网页,但为了检查链接,这是非常低效,因为它下载整个页面内容/图像/无论如何。
The probably most common use-case is to check for dead web links. For this I only need the reply code of the HTTP request and not the page content.
Getting web pages in PHP can be done easily using file_get_contents("http://...")
, but for the purpose of checking links, this is really inefficient as it downloads the whole page content / image / whatever.
推荐答案
作为curl的替代方法,您可以使用http上下文将请求方法设置为 HEAD
的选项。然后使用这些选项打开(http包装器)流并获取元数据。
As an alternative to curl you can use the http context options to set the request method to HEAD
. Then open a (http wrapper) stream with these options and fetch the meta data.
$context = stream_context_create(array('http' =>array('method'=>'HEAD')));
$fd = fopen('http://php.net', 'rb', false, $context);
var_dump(stream_get_meta_data($fd));
fclose($fd);
另见:
http://docs.php.net/stream_get_meta_data
http://docs.php.net/context.http
这篇关于在PHP中使用HTTP的HEAD命令最简单的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!