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?

2
  • is there a question hidden in there somewhere? Commented Dec 13, 2012 at 14:22
  • I assume it's "how do I see the headers returned from the curl_exec() function?" Commented Dec 13, 2012 at 14:23

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.