如何用 PHP 压缩 JSON?
我正在编写一个小分析页面,它将帮助我查找应用程序中的错误。本质上,它允许直观地比较实际数据和日志条目,并对数据进行一些分析。
由于这仅用于调试,并且由于我将在实时站点上部署它,因此我希望它的服务器负载尽可能少。一些分析选项将包括相当繁重的子字符串搜索或 n2 操作,因此我将把它卸载给客户端。
这意味着 PHP 页面只会从表和日志中获取数据(其中一些为 JSON),然后将其写出。然后,客户端 Javascript 将进行所有分析等。
问题是 JSON 数据将有几 MB 大,而且我与服务器的连接速度很慢。以某种方式压缩数据会很好。有人有想法吗?
环境为PHP+Apache;我不知道是否会安装mod_gzip;我无法控制它。
I'm writing a little analysis page which will help me hunt down bugs in an application. In essence it allows to visually compare actual data and log entries, plus perform a bit of analysis on the data.
Since this is for debugging only and since I will be deploying this on the live site I want it to have as little server load as possible. Several of the analysis options will include rather heavy substring searching or n2 operations, so I'm going to offload this to the client.
This means that the PHP page will just take the data from the tables and logs, JSON some of it, and write it out. The client Javascript will then do all the analysis etc.
The problem is that the JSON'ed data will be several MB large, and my connection to the server - slow. It would be nice to compress the data somehow. Anyone have ideas?
The environment is PHP + Apache; I don't know if mod_gzip will be installed; and I have no control over it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用PHP 的输出控制来压缩数据。只需将此调用放在脚本开头的任何输出之前即可:
现在,如果客户端接受,任何输出都将使用 gzip 或 deflate 进行压缩。
You can compress the data with PHP’s output control. Just put this call at the start of your script before any output:
Now any output will be compressed with either gzip or deflate if accepted by the client.
在 PHP 5.4 中,现在是 JSON_UNESCAPED_UNICODE,因此您可以替换 char:
\u00f3 -> Ĺ› = Ś
eq:
In PHP 5.4 is now JSON_UNESCAPED_UNICODE so you can replace char:
\u00f3 -> Ĺ› = Ś
eq:
如果您选择 apache(就像原始问题中提到的那样),您可以在 .htaccess 中添加一些规则:
If apache is your choice (and it is, like mentioned in original question), you may add some rules into .htaccess: