Decoding gzip (using PHP sockets)
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);
$line_num++;
}
// Return response as array
return $response;
}
where $this->handle is
$this->handle = fsockopen($this->host, $this->port, $this->error_number, $this->error_string);
Thank you =)
Have you tried something like...
$response='';
while(!feof($this->handle)) {
$response.=fgets($this->handle, 4096);
}
$response=gzdecode($response);
return explode("\n",$response);
Im assuming that the whole response is gzipped and not each line individually. If it's each line you just need to change fgets() into gzdecode(fgets())..
精彩评论