PHP: Simple compression of JSON data

Just discovered how super simple it was to add some gz compression when for example providing JSON data from PHP.

All you need is regular output buffering with the ob_gzhandler as output callback.

// Fetch some data
$data = get_data();

// Turn on output buffering with the gzhandler

// Output as normal
echo json_encode($data);

The cool thing is that it actually looks at what the browser accepts before doing anything.

Before ob_gzhandler() actually sends compressed data, it determines what type of content encoding the browser will accept (“gzip”, “deflate” or none at all) and will return its output accordingly. All browsers are supported since it’s up to the browser to send the correct header saying that it accepts compressed web pages.

Tried adding it for a text field with timezone auto-completing for example, and without this handler:

Content-Length    5517
Content-Type      application/json

With this handler:

Content-Encoding  gzip
Vary              Accept-Encoding
Content-Length    1775
Content-Type      application/json

Do like! 😆

  • Jiego

    Question: I see that this is totally awesome, but why wasn’t this a default for PHP from the start.

  • Jiego

    Question: I see that this is totally awesome, but why wasn’t this a default for PHP from the start?

    • Because that would be a terrible assumption to make and you’d have to turn it off whenever you didn’t want it.

  • sweet! but are there some implications regarding this implementation? say, for example, due to it being compressed, you’d still have to de-compress?

    not that big of an issue since the client will be doing the de-compression of the gz however, i’m just thinking server-side, if it increases the process more?

    i like the solution of this as to decreasing the size making it sent faster to the client.

    • Um, obviously you’ll have to decompress it, but I think the time spent for the client to do so is tiny compared to the time it takes to establish a connection and receive the file. Also, the size of the file counts in on your bandwidth use, which depending on your hosting plan and amount of visits might build up to unnecessary heights.

  • Warren

    dont forget ob_end_flush(); when you done ^^

    • Actually not needed in this case. Works perfectly fine without it. I’m guessing it happens implicitly at the end of the script 🙂

  • ChiggerChug

    Nice! Do you know of a way to store the gzipped output of this in a json file? The idea being that my request doesn’t have to run the PHP/MySQL script every time it’s needed. I’d run a cron job weekly to update the compressed json

    • I’d rather store the raw JSON in a file since this gzip handler actually looks at accept-headers and stuff like that. So it might not actually serve the JSON gzipped if the client doesn’t support it. I’d also recommend looking into cleaning up your PHP/MySQL if it’s so heavy that you feel like you need to run a weekly cron job… (I don’t know your case though, so might be a good idea too for all I know 🙂