I have a lot of static JSON text content indexed in a database. Some of this content is greater than 20K. Web pages request this content via Ajax and a call to a PHP file to fetch the data in the database.
Of course, one can gzip the returned content on the fly, but I was wondering whether I could fetch pre-gzipped content and return it directly from PHP. In other words, I could pre-gzip entries and store them in the database itself, rather than the full text.
Is this possible? If yes, how do I indicate in PHP that the returned content is gzipped binary? I use echo for plain text, but which command should I use for binary data? I am no expert at PHP. Thanks.
You would also echo the gzipped data. echo is simply a “write these bytes to the output buffer”. You can echo images if you want. The interesting thing is, what you tell the browser to do with the data. This is where the HTTP response headers come in. You need to send a Content-Encoding header. But you need to make sure, that the client is able to understand gzip, for this to work at all. You should also know, that there are two different compression algorithms in use: DEFLATE and GZIP.
You might want to consider using a reverse proxy (nginx, varnish, squid, …) and have them handle output compression.