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

查看:1396
本文介绍了如何使用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.

由于这只是为了调试,因为我将部署这在现场我希望它有尽可能少的服务器负载。一些分析选项将包括相当重的子字符串搜索或n 2 操作,因此我要将其卸载到客户端。

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');

现在,任何输出都将使用 gzip deflate (如果客户接受)。

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

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

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