如何用 PHP 压缩 JSON? [英] How to compress JSON with PHP?

查看:36
本文介绍了如何用 PHP 压缩 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小分析页面,它将帮助我寻找应用程序中的错误.从本质上讲,它允许直观地比较实际数据和日志条目,并对数据进行一些分析.

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.

由于这仅用于调试,而且我将在实时站点上部署它,因此我希望它具有尽可能少的服务器负载.几个分析选项将包括相当繁重的子字符串搜索或 n2 操作,因此我将把它卸载给客户端.

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.

这意味着 PHP 页面将只从表和日志中获取数据,其中一些是 JSON,然后将其写出.然后客户端 Javascript 将进行所有分析等.

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.

问题是 JSON 格式的数据会有几 MB 大,而且我与服务器的连接速度很慢.以某种方式压缩数据会很好.有人有想法吗?

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?

环境为PHP+Apache;不知道会不会安装mod_gzip;我无法控制它.

The environment is PHP + Apache; I don't know if mod_gzip will be installed; and I have no control over it.

推荐答案

您可以使用 PHP 的输出控制.只需在任何输出之前将此调用放在脚本的开头即可:

You can compress the data with PHP’s output control. Just put this call at the start of your script before any output:

ob_start('ob_gzhandler');

现在,如果客户端接受,任何输出都将使用 gzipdeflate 进行压缩.

Now any output will be compressed with either gzip or deflate if accepted by the client.

这篇关于如何用 PHP 压缩 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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