dprh34164 2013-05-21 00:29
浏览 57

为php脚本启用GZIP压缩

I've enabled gzip on my php scripts by doing this:

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

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

?>
<? ob_flush(); ?>

This has been working on a couple of hosts, but when I moved to Hosting24, it simply wont work.

I have also enabled 'Compress all content' in cPanel's optimize website section, along with editing the .htaccess file to something like this: https://stackoverflow.com/a/8262235/2237587

Hosting24 has assured me of this:

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

I am doing this, but it does not work for Hosting24. When I use the same script on another service (tested on 000webhost and Arvixe), the response is compressed.

What can I try next to get it compressed, to be honest I have no idea why it's not working already.

  • 写回答

1条回答 默认 最新

  • doutangu4978 2013-05-21 09:15
    关注

    Replacing this:

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

    with

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

    fixed the problem. Not sure if this is a problem with Hosting24 or not. text/javascript allows me to use the json request of AFNetworking still so i'm happy with result.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算