在 PHP 中使用 HTTP HEAD 命令最简单的方法是什么?
我想将超文本传输协议的 HEAD 命令发送到 PHP 中的服务器以检索标头,但不检索内容或 URL。我如何以有效的方式做到这一点?
最常见的用例可能是检查无效的网络链接。为此,我只需要 HTTP 请求的回复代码,而不需要页面内容。 在 PHP 中获取网页可以使用 file_get_contents("http://...")
轻松完成,但是为了检查链接,这确实效率很低,因为它会下载整个页面内容/图像/无论什么。
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 cURL 巧妙地完成此操作:
You can do this neatly with cURL:
作为curl的替代方案,您可以使用http上下文选项将请求方法设置为
HEAD
。然后使用这些选项打开(http 包装器)流并获取元数据。另请参阅:
http://docs.php.net/stream_get_meta_data
http://docs.php.net/context.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.see also:
http://docs.php.net/stream_get_meta_data
http://docs.php.net/context.http
甚至比curl更简单 - 只需使用 PHP
get_headers()
函数即可返回您指定的任何 URL 的所有标头信息的数组。检查远程文件是否存在的另一个真正简单的方法是使用fopen()
并尝试以读取模式打开 URL(您需要为此启用allow_url_fopen)。只需查看这些函数的 PHP 手册即可,全部都在那里。
Even easier than curl - just use the PHP
get_headers()
function which returns an array of all header info for any URL you specify. And another real easy way to check for remote file existence is to usefopen()
and try to open the URL in read mode (you'll need to enable allow_url_fopen for this).Just check out the PHP manual for these functions, it's all in there.
我推荐使用 Guzzle Client,它基于 CURL 库,但更简单并优化。
安装:
您的情况示例:
快速且简单。
在此处了解更多信息
I recommend using Guzzle Client, it's based on the CURL library but more simple and optimized.
installation:
example in your case:
Fast and easy.
Read more here
似乎 pear 有它:
http://pear.php .net/manual/en/package.http.http.head.php
It seems like pear has it:
http://pear.php.net/manual/en/package.http.http.head.php
就像 PHP 的补充一样,也许有人来这里寻找查看 HEAD 动词在浏览器中工作的方法,Ajax 还允许您以与 GET 和 POST 相同的方式使用它。
该信息最好的部分是 Ajax 允许我们在浏览器检查器(“网络”选项卡)中看到正在执行的实际方法 HEAD。 PHP Curl 在服务器端运行,因此,浏览器检查器将仅显示 php 文件的 GET,而不显示 curl HEAD 操作。这适用于 Ajax:
以同样的方式,当“readystatechanges”时,您可以
检索像“Content-Length”这样的标头道具,在获得状态 200 后,在同一代码块中,您可以使用以下方式检索这些道具:
Just as a complement to PHP, maybe someone came here looking for ways to see HEAD verb working in the browser, and Ajax also allows you to use it in the same way as GET and POST.
The best part of this info is that Ajax allows us to see in the browser inspector (Network tab) the actual method HEAD being executed. PHP Curl runs on the server side, so, browser inspector will show only the GET for the php file, but not the curl HEAD operation. This is for Ajax:
In the same way when "readystatechanges" you get
To retrieve header props like "Content-Length", after getting the status 200, in the same block of code, you can retrieve these props using: