2

好的,所以有人写了 Last.FM API 的 PHP 实现,我将它用于我的一个小项目。他的实现不从 Last.FM 服务器请求 gzip 压缩数据,因此我决定修改他的实现以使用 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);

谢谢你=)

4

1 回答 1

1

您是否尝试过类似...


$response='';
while(!feof($this->handle)) {
  $response.=fgets($this->handle, 4096);
}
$response=gzdecode($response);
return explode("\n",$response);

假设整个响应是压缩的,而不是单独的每一行。如果是每一行,您只需将 fgets() 更改为 gzdecode(fgets())..

于 2010-08-12T15:05:29.030 回答