My code originally worked for most websites except some like (Facebook.com). So I inserted curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
and it started working for all websites after and even getting the right results I needed.
My issue is that I am getting Notice: Undefined offset: 1
on this line:
list($k,$v)=explode(':',$header);
even though I am getting the right result back.
I have noticed that I only get that notice
when I have the CURLOPT_FOLLOWLOCATION
line but without that line, I am unable to get some websites gzip compression info
My code:
$ch = curl_init("http://facebook.com/");
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$buffer = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
$header_size = $curl_info["header_size"];
$headers = substr($buffer, 0, $header_size);
$body = substr($buffer, $header_size);
function getEncoding(&$headers){
$arr=explode("\r\n",trim($headers));
array_shift($arr);
foreach($arr as $header){
list($k,$v)=explode(':',$header);
if ('content-encoding'==strtolower($k)){
return trim($v);
}
}
return false;
}
$encoding=getEncoding($headers);
if ($encoding) {
echo "Using: ".$encoding;
}else{
echo "None";
}