为php脚本启用GZIP压缩

时间:2013-05-21 00:29:46

标签: php .htaccess gzip zlib mod-deflate

我通过这样做在我的php脚本上启用了gzip:

<? ob_start("ob_gzhandler"); 
?>
<?php
header('Content-Type: application/json');
header('Accept-Encoding: gzip');

/* Request data, transform it, json_encode it, echo */

?>
<? ob_flush(); ?>

这已经在几个主机上工作了,但是当我搬到Hosting24时,它根本无法工作。

我还在cPanel的优化网站部分启用了“压缩所有内容”,同时将.htaccess文件编辑为:https://stackoverflow.com/a/8262235/2237587

Hosting24向我保证:

Our servers support mod_gzip, mod_deflate by default. 

To use GZIP, please call the function ob_start("ob_gzhandler"); at the top of your script. Also remember to call the function ob_flush(); at end

我这样做,但它不适用于Hosting24。当我在另一个服务上使用相同的脚本(在000webhost和Arvixe上测试)时,响应被压缩。

我可以尝试下一步将其压缩,说实话我不知道它为什么不能正常工作。

1 个答案:

答案 0 :(得分:0)

取而代之:

header('Content-Type: application/json');

header('Content-Type: text/javascript');

修复了问题。不确定这是否是Hosting24的问题。 text / javascript允许我使用AFNetworking的json请求,所以我对结果感到满意。