Sunday, July 21, 2013

Decompress/Unzip Web Content with Content-Encoding: gzip (using PHP - file_get_contents() and gzinflate())

If you are using file_get_contents() to grab content from the web and process using PHP, you may bump into web contents that are compressed using gzencode.

Here's a solution to decompress those content (simplified for easy reference):


$qdata = array('http' =>
     'method' => 'POST',
     'user_agent'=> $_SERVER['HTTP_USER_AGENT']

$context = stream_context_create($qdata);

$data = file_get_contents("", false, $context);

$u = gzinflate(substr($data,10,-8));


The trick is to use gzinflate() and do remember to change the URL to your target web address. Process your $u (string that contains the HTML codes of the target) afterwards.

No comments:

Post a Comment