Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to use the HEAD command of HTTP in PHP?

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.

like image 200
fuenfundachtzig Avatar asked Oct 09 '09 18:10

fuenfundachtzig


People also ask

How do you use HTTP HEAD method?

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.

How do I make a HTTP HEAD request?

To make a HEAD request with Curl, you need to use the -I or --head command-line parameter. The -I command-line parameter tells Curl to send an HTTP HEAD request to receive only HTTP headers. The HEAD request is very similar to a GET request, except that the server only returns HTTP headers without a response body.

What is the head in HTTP?

HEAD is a request method supported by HTTP used by the World Wide Web. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

How do you handle a head request?

HEAD request can be handled as if it was a GET request. You can access all parameters the same way - via $_GET or $_REQUEST superglobals. The main difference is that your script should not return any content, if you want to adhere to HTTP specification.


2 Answers

You can do this neatly with cURL:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");

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

// grab URL and pass it to the browser
curl_exec($ch);

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

// close cURL resource, and free up system resources
curl_close($ch);
like image 98
PatrikAkerstrand Avatar answered Oct 07 '22 14:10

PatrikAkerstrand


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

like image 27
VolkerK Avatar answered Oct 07 '22 14:10

VolkerK