Zip JSON object before submitting to PHP

I am interested in cutting text from a JSON object on the server before transferring it to my mobile device that requested the object. A small tipping zipping test will reduce its size by about 80%! This is great for mobile! :)

I don’t need to save the zip file that I create on the server at all, just stay in memory and then repeat it. I can unzip it on the android side without problems.

In any case, I did a little manipulation, but I could not come up with anything that works, here is what I still have:

while($e=mysql_fetch_assoc($q)) $output[]=$e; $zip = new ZipArchive(); $zip->addFromString("test",(json_encode($output))); echo $zip; 

I know that I'm probably doing something massively wrong, I'm not very familiar with php. My $ q is a cursor containing many sql rows, and if I use print(json_encode($output)); instead of all zip-shenanigans, it works great for outputting raw text.

I suppose this should not be a ZIP compression, but any compression would be helpful if you could point me in the right direction, I could probably figure it out. Thanks!

+6
source share
2 answers

you can use ob_start with ob_gzhandler :

 if(function_exists('ob_gzhandler')) ob_start('ob_gzhandler'); else ob_start(); echo json_encode($output); ob_end_flush(); 
+9
source

Server side: create a JSON object that contains only the property, which is an array of bytes, which is a JSON object, zipped and sends it to the client. Client Side: Unzip the byte array contained in the object to get the original JSON data. Pro: 5X The fastest communication client server, less IO for the server. Requires more processor work from the server. Take your attention: P

0
source

Source: https://habr.com/ru/post/891349/


All Articles