解码 gzip(使用 PHP 套接字)

发布于 2024-09-13 21:53:36 字数 782 浏览 5 评论 0原文

好的,有人编写了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2024-09-20 21:53:36

您是否尝试过类似的操作...


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

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

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())..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文