15

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.

fuenfundachtzig
  • 7,952
  • 13
  • 62
  • 87

5 Answers5

28

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);
PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
21

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

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • 1
    I prefer this solution over the ones using curl, because I like using built-in functions. Maybe somebody else can comment on the performance of each possibility? – fuenfundachtzig Oct 23 '09 at 10:30
  • Note that this will throw an error for `401` response codes, while curl provides you with the actual response. – Fabian Schmengler Dec 14 '15 at 11:06
  • 2
    `stream_context_create()` can be used also with `file_get_contents()`. Maybe `get_headers()` is much better combined with `stream_context_set_default()` to the method of the request to HEAD. See http://php.net/manual/es/function.get-headers.php – cybmeta Feb 03 '16 at 16:56
5

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.

Brian
  • 2,107
  • 6
  • 22
  • 40
  • 7
    `get_headers()` will actually send a 'GET' request unless you do this first: `stream_context_set_default(array('http'=>array('method'=>'HEAD')));` – Octopus Jun 01 '17 at 03:02
3

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

Amin Shojaei
  • 5,451
  • 2
  • 38
  • 46
2

It seems like pear has it:

http://pear.php.net/manual/en/package.http.http.head.php

JCasso
  • 5,423
  • 2
  • 28
  • 42