[英]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;
上面的網站測試它。 如果我完全以錯誤的方式解決這個問題,請告訴我。
====
對於其他試圖實現相同功能的人來說,最好只使用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.