How to compress JSON with PHP?

34,367

Solution 1

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

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

Solution 2

In PHP 5.4 is now JSON_UNESCAPED_UNICODE so you can replace char:

\u00f3 -> Ĺ› = Ś

eq:

 json_encode($data,JSON_UNESCAPED_UNICODE);

Solution 3

If apache is your choice (and it is, like mentioned in original question), you may add some rules into .htaccess:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    # Add any mime-type you think is appropriate here
    AddOutputFilterByType DEFLATE application/json
</IfModule>
Share:
34,367

Related videos on Youtube

Vilx-
Author by

Vilx-

Just your average everyday programmer. #SOreadytohelp

Updated on September 23, 2020

Comments

  • Vilx-
    Vilx- over 3 years

    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.

  • Frank Farmer
    Frank Farmer over 14 years
    +1 -- definitely the way to go. Note, however, that PHP must be configured with --with-zlib for this to work us2.php.net/manual/en/zlib.installation.php
  • j4k3
    j4k3 over 7 years
    This indeed makes a JSON string transfer as compressed data, but at a compression factor of 1.00x, length reduced by 0 bytes on 300kb json data with little entropy. I've written a tiny test script that just sets the Content-Type header to "application/json", then initializes the output buffer, then transfers the json data with echo json_encode(...). Any idea what am I doing wrong?