繁体   English   中英

如何使用PHP Simple HTML DOM Parser启用gzip压缩

[英]How to enable gzip compression using PHP Simple HTML DOM Parser

我已经尝试了一些使用PHP Simple HTML DOM Parser来启用gzip压缩的东西,但到目前为止似乎没有任何工作。 使用ini_set我已经修改了用户代理,所以我认为也可以启用gzip压缩?

include("simpdom/simple_html_dom.php");
ini_set('zlib.output_compression', 'On');   
$url = 'http://www.whatsmyip.org/http_compression/';
$html = file_get_html($url);
print $html;

上面的网站测试它。 如果我完全以错误的方式解决这个问题,请告诉我。

====

UPDATE

对于其他试图实现相同功能的人来说,最好只使用cURL,然后像这样使用dom解析器:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // Define target site
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Return page in string
curl_setopt($cr, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2');
curl_setopt($ch, CURLOPT_ENCODING , "gzip");     
curl_setopt($ch, CURLOPT_TIMEOUT,5); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects

$return = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 

$html = str_get_html("$return");

CURLOPT_ENCODING是响应返回 (接受为)gzip压缩数据 - 服务器设置(ob_start(“ob_gzhandler”)或php_ini ..)告诉服务器输出gzip压缩数据。

就像你使用不支持gzip的浏览器访问该页面一样。 接受 gzip数据,您必须使用curl,以便进行区分。

只需在输出数据的PHP脚本的最顶部添加以下行:

  ob_start("ob_gzhandler");

参考

------- --------更新

您还可以尝试通过.htaccess文件在全站点启用gzip Compresion。 像这样的东西应该gzip你的网站内容,但图像:

# Insert filter
SetOutputFilter DEFLATE

# Netscape 4.x has some problems...
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

# Don't compress images
#SetEnvIfNoCase Request_URI \
\.(?:gif|jpe?g|png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM