解码 gzip(使用 PHP 套接字)
好的,有人编写了 Last.FM API 的 PHP 实现,我将其用于我的一个小项目。他的实现不请求来自 Last.FM 服务器的 gzip 数据,因此我决定修改他的实现以与 gzip 配合使用以减少带宽。我在请求压缩数据时没有问题,效果很好,并且所有数据都经过压缩(检查)。问题在于解码它。我对 PHP 还很陌生,过去两天我一直在尝试解码它,但我尝试的任何方法都不起作用。 :D
这是请求和接收数据的函数。如果有人可以帮助我使这个函数解码数据,我将非常感激。
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;
}
其中 $this->handle 是
$this->handle = fsockopen($this->host, $this->port, $this->error_number, $this->error_string);
谢谢 =)
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 =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过类似的操作...
我假设整个响应被压缩,而不是单独的每一行。如果是每一行,您只需将 fgets() 更改为 gzdecode(fgets()) ..
Have you tried something like...
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())..