Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get send gzip content as PHP response

Tags:

php

my ajax response is very large its actually 900kb. so I need to gzip to improve the performance.

can anyone please help me to get gziped content from my php.

Thanks in advance.

like image 790
harishkumar329 Avatar asked Sep 27 '13 05:09

harishkumar329


People also ask

How do you gzip a body request?

To compress the HTTP request body, you must attach the HTTP header indicating the sending of HTTP request body compressed in gzip format while sending the request message from the Web Service client. Implement the processing for attaching the HTTP header in the client application.

How do I enable the compressed HTML content transfer on your server using gzip?

The easiest way to enable GZIP compression on your WordPress site is by using a caching or performance optimization plugin. For example, if you host your WordPress site on Apache web server, W3 Total Cache includes an option to enable GZIP compression under its Browser Cache settings panel.

What is gzip encoding?

Gzip is a file format and software application used on Unix and Unix-like systems to compress HTTP content before it's served to a client.


3 Answers

Before outputing anything on the page throw in this:

ob_start("ob_gzhandler");

Then after your content follow it up with:

ob_end_flush();

And your content will be gzipped

like image 122
Chase Avatar answered Nov 09 '22 00:11

Chase


Option 1:

configure zlib.output_compression in your php.ini

Doc: http://www.php.net/manual/en/zlib.configuration.php#ini.zlib.output-compression

Option 2:

Use : ob_gzhandler

Doc: http://php.net/manual/en/function.ob-gzhandler.php

Note:

900Kb is too much for an ajax response.

like image 37
trrrrrrm Avatar answered Nov 09 '22 02:11

trrrrrrm


If you cannot update your php.ini config file before using ob_gzhandler() you could try this too:

ini_set('zlib.output_compression', 1);
like image 13
Real Avatar answered Nov 09 '22 00:11

Real