Sunday, November 29, 2009

Simple Header Reader

Well I tried doing a search on this myself but after many unsuccessful searches I decided to tackle the task myself (I really thought I would find something quickly). I figured it would be simple so someone must have done something on the topic before and I proved myself wrong - well about the finding part, it was rather simple.

Anyway what this little function does is read only the headers of a page using PhP's curl() functions and returns them as an associative array. A great way to check if you need to update an RSS cache or something similar.

function checkHeaders($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$content = curl_exec($ch);
curl_close($ch);
preg_match_all('/[^:]*[\r\n]|([^:]*):(.*)[\r\n]/', $content, $matches, PREG_SET_ORDER);
foreach ( $matches as $match ) {
if ( !empty($match[0]) ) {
if( strstr($match[0], 'HTTP') === false )
$headers[$match[1]] = $match[2];
else
$headers['HTTP'] = $match[0];
}
}
return $headers;
}

The HTTP header index will simply be 'HTTP' every other header will be in the form <header> => <value>.
example:

['Content-Type'] => ['text/xml']

enjoy :)

No comments: