curl header gzip 区分头部和压缩内容。

#PHP读gzip流
if (!function_exists('gzdecode')) { 
	function gzdecode ($data) { 
		$flags = ord(substr($data, 3, 1)); 
		$headerlen = 10; 
		$extralen = 0; 
		$filenamelen = 0; 
		if ($flags & 4) { 
		$extralen = unpack('v' ,substr($data, 10, 2)); 
		$extralen = $extralen[1]; 
		$headerlen += 2 + $extralen; 
		} 
		if ($flags & 8) // Filename 
		$headerlen = strpos($data, chr(0), $headerlen) + 1; 
		if ($flags & 16) // Comment 
		$headerlen = strpos($data, chr(0), $headerlen) + 1; 
		if ($flags & 2) // CRC at end of file 
		$headerlen += 2; 
		$unpacked = @gzinflate(substr($data, $headerlen)); 
		if ($unpacked === FALSE) 
		$unpacked = $data; 
		return $unpacked; 
	} 
} 

$url="http://xxx.com";//返回压缩数据的网址
$file=dirname(__FILE__).'/tmp.htm';
$c=new curl($url);
$c->set(CURLOPT_HEADER,1);
$c->file($file);
echo $content=$c->exec();
echo "<hr>";
$info=$c->getinfo();
echo "<hr>11";
print_R($info);
$header=substr($content,0,$info[header_size]);
echo $header.'<hr>';
$body=substr($content,$info[header_size]);
echo htmlspecialchars(gzdecode($body)).'<hr>';

你可能感兴趣的:(c,PHP,function,header,File,url)