Accessibility Navigation

Get the headers of a HTTP request with PHP

Posted on 20th January 2011 by Jacob Wyke

Need to check that a URL is valid? Or get the last-modified date, or the charset, or the ETag of a page? Here is a simple bit of code to help you do just that by returning the headers of a HTTP request for you.

The following PHP code is the equivalent of ‘curl -I‘:

<?php
 
$strURL = "www.webvamp.co.uk";
 
$resCurl = curl_init();
 
//set URL and other appropriate options
curl_setopt($resCurl, CURLOPT_URL, $strURL);
curl_setopt($resCurl, CURLOPT_HEADER, true);
curl_setopt($resCurl, CURLOPT_NOBODY, true);
curl_setopt($resCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($resCurl, CURLOPT_FOLLOWLOCATION, true);
 
//get the headers
$strHeaders = curl_exec($resCurl);
 
//close cURL
curl_close($resCurl);
 
echo $strHeaders;
 
?>

You will of course need the cURL library compiled with PHP.


Have Your Say

Have Your Say Form









Comments

  • Be the first to add your voice to this post!

    Start the discussion and leave your views or thoughts by writing a comment using the form above.