Get HTTP headers of any site using curl

Get all HTTP headers of any website with Curl. It is very simple as curl is a very powerful library of php. Look at the given code.

If you want to know more about curl, You may also want to take a look at Working of Curl.

    $url = '';

    $agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080311 Firefox/";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);

    $data = explode("\n",trim($output));

    echo "<pre>";
    foreach($data as $part){
      if(stripos($middle[0],'HTTP') === false){
       echo "<b>".trim($middle[0])."</b>=>".trim($middle[1]);
       echo "<b>Status</b>"."=>".trim($middle[0]);
    echo "\n";
    echo "</pre>";

One thing to note here that to get only headers, set the status of CURLOPT_NOBODY to true otherwise bydefault it will return full body contents.

If you want to return the contents of body also you can omit this statement or set its status to false.
Get HTTP headers of any site using curl Get HTTP headers of any site using curl Reviewed by Altaf Hussain on 9:04 PM Rating: 5


  1. Super thanks! I was looking for the better way to implement Curl to view the HTTP header and this is perfect :)


Altaf Web. Powered by Blogger.