Content-Encoding: gzip的网站内容用gzuncompress不能解压,怎样办?在线等
关键词:Content Encoding gzip,gzuncompress,gzip,PHP,zlib,error,ewing
ewing:
Content-Encoding: gzip的网站内容用gzuncompress不能解压,怎样办?在线等
复制PHP内容到剪贴板PHP代码:
<?php
$url="http://...";
$source_file="index.txt";
$fp = fopen($source_file, "w");
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,false);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
用以上代码拿某个网站的页面保存下来,发现是乱码。查看一下头,原来是经过压缩的。
Content-Encoding: gzip
上网查了一下安装了zlib库,想打文件解压,但是没成功,提示:Warning: gzuncompress() [function.gzuncompress]: data error复制PHP内容到剪贴板PHP代码:
<?
$filename = "./index.txt";
$zd = gzopen($filename, "r");
$contents = gzread($zd, filesize ($filename));
gzclose($zd);
file_put_contents('./gz.txt',$contents);
#--------------------------------------------------
$filename = "index.txt";
$contents = file_get_contents($filename);
$contents = gzuncompress($contents);
file_put_contents('./gz.txt',$contents);
?>
是我解压的方法不对,还是我程序出错了?请指教。
[ 本帖最后由 ewing 于 2008-1-16 11:59 编辑 ]
ewing:
oh,解决了,原来压缩文件保存时不应当加上头部信息。希望大家引觉得戒。