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.
$data = get_data();
// Turn on output buffering with the gzhandler
ob_start('ob_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-Type application/json
With this handler:
Vary Accept-Encoding
Content-Length 1775
Content-Type application/json
Do like!
'웹프로그래밍 > PHP' 카테고리의 다른 글
[PHP 7.2] mcrypt_create_iv, mcrypt_encrypt, mcrypt_decrypt is undefined function (0) | 2018.08.28 |
---|---|
php : Thread Safe 와 Non Thread Safe 차이점 (0) | 2017.12.02 |
How to call PHP function from string stored in a Variable (0) | 2016.05.24 |
HTTP GET String + 등이 삭제되었을 때 (0) | 2016.04.18 |
Centos 7 / Apache / PHP - mkdir(): Permission denied (0) | 2016.01.20 |