What is the easiest way to use the HEAD command of HTTP in PHP?
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
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);