How to enable gzip compression using PHP Simple HTML DOM Parser

I tried a few things to enable gzip compression using PHP Simple HTML DOM Parser, but so far nothing works. Using ini_set, I was able to change the user agent, so I decided it was possible to also enable gzip compression?

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; 

The website above is checking it out. Please let me know if I am completely mistaken.

====

UPDATE

For anyone trying to achieve the same, it's best to use cURL and then use the dom parser like this:

 $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"); 
+4
source share
2 answers

CURLOPT_ENCODING is that the response is returned (accepted as) gzipped data - server settings (ob_start ("ob_gzhandler") or php_ini ..) inform the server about the outputs using gps OUTPUT files.

Just as if you went to this page with a browser that did not support gzip. To accept gzip data, you must use curl so you can make this distinction.

+1
source

Just add the following line to the very top of the PHP script that displays the data:

  ob_start("ob_gzhandler"); 

Link

------- -------- Update

You can also try enabling gzip Compresion sitewide via a .htaccess file. Something like this should gzip the content of your sites, but images:

 # 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
source

Source: https://habr.com/ru/post/1310991/


All Articles