Skip to content

Instantly share code, notes, and snippets.

@streamsend
Created June 15, 2010 17:01
Show Gist options
  • Select an option

  • Save streamsend/439378 to your computer and use it in GitHub Desktop.

Select an option

Save streamsend/439378 to your computer and use it in GitHub Desktop.
How to parse headers from a CURL response
<?
class CurlResponse
{
var $headers = array();
function CurlResponse () {}
function parse_header ($handle, $header)
{
$details = split(':', $header, 2);
if (count($details) == 2)
{
$key = trim($details[0]);
$value = trim($details[1]);
$this->headers[$key] = $value;
}
return strlen($header);
}
}
$response = new CurlResponse();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$response, 'parse_header'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
print_r($response->headers);
?>
@AfreenTech33
Copy link

this is not returning the location header correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment