How to compress JSON with PHP?

﹥>﹥吖頭↗ 提交于 2019-11-28 17:57:19
Gumbo

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!