0

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";
}
user4756836
  • 1,277
  • 4
  • 20
  • 47
  • That error means there's no `:` in `$header`. Try echoing `$header` when this happens. – Barmar Mar 03 '16 at 02:44
  • There are multiple `:` in the `$header`. Can't paste the whole output here due to it being very long @Barmar – user4756836 Mar 03 '16 at 02:47
  • @barmar http://stackoverflow.com/questions/9183178/php-curl-retrieving-response-headers-and-body-in-a-single-request#comment30757650_9183276 that is the message I am getting – user4756836 Mar 03 '16 at 02:50
  • 1
    Errors don't happen for no reason. That error can only happen if `$header` doesn't have `:` in it. Put `if(!strstr($header, ':')) { echo "Bad header: $header"; }` in the loop. – Barmar Mar 03 '16 at 03:00

1 Answers1

1

Check whether the header contains : before calling explode.

foreach($arr as $header){
    if (strstr($header, ':')) {
        list($k,$v)=explode(':',$header);
        if ('content-encoding'==strtolower($k)){
            return trim($v);
        }
    }
}

This will ignore any headers that aren't of the form Name: value. This could happen if the response starts with 100 Continue, as the 200 HTTP/1.1 will be mixed into it later.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I see the issue here... but my question is... is it okay to skip over some of the headers if I am just trying to check the gzip compression for the website? – user4756836 Mar 03 '16 at 03:12
  • Sure. You don't care about anything else. – Barmar Mar 03 '16 at 03:13
  • But what if one of the 100 Continue response has the info about the gzip? @barmar – user4756836 Mar 03 '16 at 03:37
  • I don't know what you mean by that. The loop doesn't stop when it sees that, it just skips over it and goes on to the next header line. – Barmar Mar 03 '16 at 03:39