2

I've been using PHP curl to get data I need from remote website. Here is the cURL function I used:

function get_content($adr)  
    {  
       $ch = curl_init();  

       curl_setopt ($ch, CURLOPT_URL, $adr);  
       curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
       curl_setopt ($ch, CURLOPT_HEADER, 0);  

       ob_start();  

       curl_exec ($ch);  
       curl_close ($ch);  
       $string = ob_get_contents();  

       ob_end_clean();  

       return $string;      

    }  
$myrul = "http://remoteurl.com";
$result = get_content($myrul);

But how do I get the headers for the response?

Fenton
  • 241,084
  • 71
  • 387
  • 401
DadaB
  • 762
  • 4
  • 12
  • 29

1 Answers1

3

If my comment above is correct, change:

curl_setopt($ch, CURLOPT_HEADER, 0);

to:

curl_setopt($ch, CURLOPT_HEADER, 1);

and parse the returned headers out however you see fit. Note that just changing the above in your function will return both headers and content, so if you want only headers returned:

function http_head_curl($url,$timeout=10)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // in seconds
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    if ($res === false) {
        throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
    }
    return trim($res);
}
WWW
  • 9,734
  • 1
  • 29
  • 33