Home > PHP, Uncategorized > Allow ZLIB compression in CURL and PHP

Allow ZLIB compression in CURL and PHP

August 6th, 2010 Leave a comment Go to comments

function file_get_contents_utf8($URL)
{
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_HTTPHEADER, array("Accept-Encoding: gzip, deflate","Accept: */*"));
$ret = curl_exec($c);
curl_close($c);

if(strpos($ret, "\r\n\r\n")){
$add = 4;
}else{
$add = 0;
}

$m_body = substr($ret, strpos($ret, "\r\n\r\n")+$add );
$m_header = substr($ret, 0, -strlen($m_body));

if(strstr($m_header,"Content-Encoding: gzip")){
$contents = gzinflate(substr($m_body,10));
}else{
$contents = $m_body;
}

if ($contents) return $contents;
else return FALSE;
}

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Categories: PHP, Uncategorized Tags:
  1. No comments yet.