php
  • dom
  • gzip
  • 2010-05-26 44 views 4 likes 
    4

    我已经尝试了一些使用PHP简单的HTML DOM解析器启用gzip压缩的东西,但目前为止似乎没有任何工作。使用ini_set我已经管理更改用户代理,所以我认为它也可以启用gzip压缩?如何使用PHP简单的HTML DOM解析器启用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

    其他任何人试图达到同样的事情,最好只使用卷曲,然后使用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压缩您的网站的内容,但图像:

    # 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 2010-05-26 16:38:24

    +0

    Pablo - 伟大的代码:)请记住,尽管如此,他在“请求”gzip内容,而不是在这种情况下发送它。他将去另一台服务器,询问数据并试图说“给我压缩,我可以处理它”。 – 2010-05-27 02:01:12

    1

    CURLOPT_ENCODING是,这样的响应来(接受)gzip压缩的数据 - 服务器设置(ob_start(“ob_gzhandler”)或php_ini ..)告诉服务器OUTPUT gzipped数据。

    就像如果您使用不支持gzip的浏览器转到该页面一样。要接受 gzip数据,您必须使用curl,以便您可以做出区分。

    +0

    谢谢澄清丹。我用file_get_html测试了你的方法,但它仍然不起作用。 似乎没有捷径,必须先使用卷曲。 – brant 2010-05-26 22:01:47

    +0

    那么,这真的是file_get_contents,但认为它值得一试。 – 2010-05-26 22:48:04

    相关问题