How to compress JSON with PHP? How to compress JSON with PHP? apache apache

How to compress JSON with 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');

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


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

\u00f3 -> Ĺ› = Ś

eq:

 json_encode($data,JSON_UNESCAPED_UNICODE);


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>