2010-05-26 7 views
4

PHP 간단한 HTML DOM 파서를 사용하여 gzip 압축을 사용하기 위해 몇 가지 시도했지만 지금까지는 아무 것도 작동하지 않는 것 같습니다. ini_set을 사용하여 사용자 에이전트를 변경해야했기 때문에 gzip 압축을 사용하는 것이 가능할 수도 있습니다.PHP를 사용하여 gzip 압축을 사용하는 방법 간단한 HTML DOM 파서

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

, 그것은 단지 컬을 사용하는 것이 가장 좋습니다, 다음과 같이 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"); 

답변

0

그냥 데이터를 출력하는 PHP 스크립트의 맨 위에 다음 행을 추가하십시오.

ob_start("ob_gzhandler"); 

Reference

------- 업데이트 --------

또한 .htaccess 파일을 통해 GZIP의 Compresion의 사이트 전체를 사용하도록 시도 할 수 있습니다. 이 같은 뭔가 콘텐츠 사이트 만 이미지를 gzip을해야 응답이 다시 (로 인정) 데이터 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 
+0

그러나 압축 테스트 페이지에 따라 응답 해 주셔서 감사합니다. . 여전히 작동하지 않는다고 말합니다. 압축을 사용할 수있는 유일한 방법은 cURL을 사용하는 것입니다. \t curl_setopt ($ ch, CURLOPT_ENCODING, "gzip"); 다른 아이디어가 있습니까? – brant

+0

파블로 - 훌륭한 코드 :) 그래도 그는 gzip 콘텐츠를 "요청"하고 있습니다.이 경우에는 보내지 않습니다. 그는 다른 서버로 가서 데이터를 요청하고 "압축 해 줘"라고 말하려고합니다. –

1

CURLOPT_ENCODING는 (-)위한 ob_start ("ob_gzhandler를"또는 php_ini ..) 서버 설정을 OUTPUT gzipped 데이터를 서버에 전달하십시오.

gzip을 지원하지 않는 브라우저에서 해당 페이지로 이동 한 경우와 같습니다. gzip 데이터를 허용하려면 해당 구분을 할 수 있도록 컬을 사용해야합니다.

+0

Dan님께 감사드립니다. 나는 당신의 방법을 file_get_html로 테스트했으나 여전히 효과가 없었다. 바로 가기가없고 컬을 먼저 사용해야하는 것처럼 보입니다. – brant

+0

글쎄, 그건 정말로 file_get_contents에 대한 것이지만, 그럴 가치가 있다고 생각 .. –

관련 문제