在 PHP 中使用 HTTP HEAD 命令最简单的方法是什么?

发布于 2024-08-07 11:51:51 字数 237 浏览 7 评论 0原文

我想将超文本传输​​协议的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赏烟花じ飞满天 2024-08-14 11:51:51

您可以使用 cURL 巧妙地完成此操作:

<?php
// create a new cURL resource with the url
$ch = curl_init( "http://www.example.com/" );     

// This changes the request method to HEAD
curl_setopt($ch, CURLOPT_NOBODY, true);

// Execute curl with the configured options
curl_exec($ch);

// Edit: Fetch the HTTP-code (cred: @GZipp)
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

// To print the response code:
print( $code);

// To check the size/length:
$length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); 

// To print the content_length:
print( "<br>\n $length bytes");


// close cURL resource, and free up system resources
curl_close($ch);

You can do this neatly with cURL:

<?php
// create a new cURL resource with the url
$ch = curl_init( "http://www.example.com/" );     

// This changes the request method to HEAD
curl_setopt($ch, CURLOPT_NOBODY, true);

// Execute curl with the configured options
curl_exec($ch);

// Edit: Fetch the HTTP-code (cred: @GZipp)
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 

// To print the response code:
print( $code);

// To check the size/length:
$length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); 

// To print the content_length:
print( "<br>\n $length bytes");


// close cURL resource, and free up system resources
curl_close($ch);
自我难过 2024-08-14 11:51:51

作为curl的替代方案,您可以使用http上下文选项将请求方法设置为HEAD。然后使用这些选项打开(http 包装器)流并获取元数据。

$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

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);

see also:
http://docs.php.net/stream_get_meta_data
http://docs.php.net/context.http

春夜浅 2024-08-14 11:51:51

甚至比curl更简单 - 只需使用 PHPget_headers() 函数即可返回您指定的任何 URL 的所有标头信息的数组。检查远程文件是否存在的另一个真正简单的方法是使用fopen()并尝试以读取模式打开 URL(您需要为此启用allow_url_fopen)。

只需查看这些函数的 PHP 手册即可,全部都在那里。

Even easier than curl - just use the PHPget_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.

小兔几 2024-08-14 11:51:51

我推荐使用 Guzzle Client,它基于 CURL 库,但更简单优化

安装:

composer require guzzlehttp/guzzle

您的情况示例:

// create guzzle object
$client = new \GuzzleHttp\Client();

// send request
$response = $client->head("https://example.com");

// extract headers from response
$headers = $response->getHeaders();

快速且简单。

在此处了解更多信息

I recommend using Guzzle Client, it's based on the CURL library but more simple and optimized.

installation:

composer require guzzlehttp/guzzle

example in your case:

// create guzzle object
$client = new \GuzzleHttp\Client();

// send request
$response = $client->head("https://example.com");

// extract headers from response
$headers = $response->getHeaders();

Fast and easy.

Read more here

稳稳的幸福 2024-08-14 11:51:51

就像 PHP 的补充一样,也许有人来这里寻找查看 HEAD 动词在浏览器中工作的方法,Ajax 还允许您以与 GET 和 POST 相同的方式使用它。

该信息最好的部分是 Ajax 允许我们在浏览器检查器(“网络”选项卡)中看到正在执行的实际方法 HEAD。 PHP Curl 在服务器端运行,因此,浏览器检查器将仅显示 php 文件的 GET,而不显示 curl HEAD 操作。这适用于 Ajax:

xmlhttp.open("HEAD", url);
xmlhttp.send();

以同样的方式,当“readystatechanges”时,您可以

xmlhttp.status // (200 meaning 'OK, success', 404 'not found', etc)

检索像“Content-Length”这样的标头道具,在获得状态 200 后,在同一代码块中,您可以使用以下方式检索这些道具:

 xmlhttp.getResponseHeader('Content-Length');
 // and for all:
 xmlhttp.getAllResponseHeaders();

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:

xmlhttp.open("HEAD", url);
xmlhttp.send();

In the same way when "readystatechanges" you get

xmlhttp.status // (200 meaning 'OK, success', 404 'not found', etc)

To retrieve header props like "Content-Length", after getting the status 200, in the same block of code, you can retrieve these props using:

 xmlhttp.getResponseHeader('Content-Length');
 // and for all:
 xmlhttp.getAllResponseHeaders();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文