【问题标题】:Enabling GZIP compression for php script为 php 脚本启用 GZIP 压缩
【发布时间】:2013-05-21 17:15:47
【问题描述】:

我通过这样做在我的 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 上测试)时,响应被压缩了。

接下来我可以尝试什么来压缩它,老实说,我不知道为什么它已经不起作用了。

【问题讨论】:

  • 感谢您的建议,但没有成功
  • GZIP 在我将内容类型设置为 text/html 时有效,但我需要将内容类型设置为 application/json 才能与 AFNetworking 很好地配合使用。这是 Hosting24 不接受 application/json 作为内容类型的问题吗?

标签: php .htaccess gzip zlib mod-deflate


【解决方案1】:

替换这个:

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

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

解决了这个问题。不确定这是否是 Hosting24 的问题。 text/javascript 仍然允许我使用 AFNetworking 的 json 请求,所以我对结果很满意。

【讨论】:

    猜你喜欢
    • 2011-10-11
    • 2011-02-28
    • 2013-12-29
    • 2013-05-05
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    相关资源
    最近更新 更多