View Full Version : Decoding gzip (using PHP sockets)

12 Aug 2010, 11:25 AM
Ok, so there is this PHP implementation of Last.FM API some guy wrote and I'm using it for a small project of mine. His implementation doesn't request gzipped data from Last.FM servers so I decided to modify his implementation to work with gzip to reduce bandwidth. I have no problem in requesting gzipped data, that works just fine and all the data comes compressed (checked). The problem is decoding it. I'm pretty new with PHP and I was trying to decode it for the last two days but nothing I tried worked. :D

Here is the function that asks for and receives the data. If anyone could please help me make this function decode the data I'd be really grateful.

function send ($msg) {
// Send message over connection
fwrite($this->handle, $msg);

$response = array();
$line_num = 0;
while ( !feof($this->handle) ) {
$response[$line_num] = fgets($this->handle, 4096);

// Return response as array
return $response;

where $this->handle is:

$this->handle = fsockopen($this->host, $this->port, $this->error_number, $this->error_string);

Of course I did some google-ing first and here are a few solutions people recommend I tried that didn't work out (for me):

gzdecode() gives me "Fatal error: Call to undefined function gzdecode()"
After google-ing it I found out gzdecode() will be implemented in php6? (on localhost I have 5.3.0 installed). I also tried using this simple function I found here (php.net/manual/en/function.gzdecode.php)

function gzdecode($data){
return $d;

This one doesn't get the job done, it decodes nothing but only returns funny characters..

gzi***ate (and also gzuncompress) function gives me "Warning: gzi***ate() [function.gzi***ate]: data error"

Thank you =)