使用GZIP下载文件 [英] Downloading files using GZIP

查看:66
本文介绍了使用GZIP下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多XML-s,我使用file或file_get_content进行下载,但是服务器管理员告诉我,通过GZIP进行下载更有效.我的问题是我如何包含GZIP,因为我以前从未做过,所以这个解决方案对我来说真的是新的.

I have many XML-s and I downloaded using file or file_get_content, but the server administrator told me that through GZIP is more efficient the downloading. My question is how can I include GZIP, because I never did this before, so this solution is really new for me.

推荐答案

我不明白您的问题.

您说您下载了这些文件-您不能单方面启用客户端压缩.

You say that you downloaded these files - you can't unilaterally enable compression client-side.

OTOH,您可以在服务器端进行控制-并且由于您已将问题标记为PHP,因此管理员在没有压缩的地方推荐压缩没有任何意义控制服务器,那么我认为这就是您在说的.

OTOH you can control it server-side - and since you've flagged the question as PHP, and it doesn't make any sense for your administrator to recommend compression where you don't have control over the server then I assume this is what you are talking about.

在这种情况下,您只需执行以下操作即可:

In which case you'd simply do something like:

<?php
ob_start("ob_gzhandler");
...your code for generating the XML goes here

...或者也许与PHP无关,并且XML文件是静态的-在这种情况下,您需要配置网络服务器以进行即时压缩.

...or maybe this is nothing to do with PHP, and the XML files are static - in which case you'd need to configure your webserver to compress on the fly.

除非您表示服务器上可以使用压缩功能,并且您要使用PHP作为客户端通过HTTP来获取数据-在这种情况下,只有在客户端提供"Accept-Encoding"请求标头(包括"gzip".在这种情况下,可以使用file_get_contents()代替

Unless you mean that compression is available on the server and you are fetching data over HTTP using PHP as the client - in which case the server will only compress the data if the client provides an "Accept-Encoding" request header including "gzip". In which case, instead of file_get_contents() you might use:

function gzip_get_contents($url)
{
     $ch=curl_init($url);
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $content=curl_exec($ch);
     curl_close($ch);
     return $content;
}

这篇关于使用GZIP下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆