1. Comparison of HTTP output with compression and not using compression
2. Turn on gzip
Use Apache mod_deflate module to open gzip
To open a method:
sudo a2enmod deflatesudo/etc/init.d/apache2 restart
Close method:
sudo a2dismod deflatesudo/etc/init.d/apache2 restart
3. Set the type of output that requires gzip compression
The output type of JSON is Application/json, so you can set this
Join in the httpd.conf <Directory></Directory>
<ifmodule Mod_deflate.c>addoutputfilterbytype deflate Application/json</ifmodule>
<?php$data = Array (' name ' = ' = ' one ', ' value ' =>1), Array (' name ' = ' = ') ', ' value ' =>2), Array (' name ' = ' three ', ' value ' =>3), Array (' name ' = ' four ', ' value ' =>4), Array (' name ' = > ' Five ', ' value ' =>5), Array (' name ' = = ' six ', ' value ' =>6), Array (' name ' = ' = ' seven ', ' value ' = >7), Array (' name ' = ' eight ', ' value ' =>8), Array (' name ' = ' = ' nine ', ' value ' =>9), Array (' Name ' = ' ten ', ' Value ' =>10), header (' Content-type:application/json '); Echo Json_encode ($data);? >
To set the pre-gzip output:
After setting the GZIP output:
4. Single JSON compressed output using gzip
After you set Addoutputfilterbytype DEFLATE Application/json, all data output in JSON format will use gzip compression output.
If you only want to use gzip compression output for one JSON, you can use the Ob_start () method to implement the other.
First, you don't need to set up Addoutputfilterbytype, and then add it at the beginning of the codeob_start('ob_gzhandler');
<?phpob_start (' Ob_gzhandler '); $data = Array (' name ' = ' = ' one ', ' value ' =>1), Array (' name ' = = ' Both ', ' value ' =>2), Array (' name ' = ' = ' three ', ' value ' =>3), Array (' name ' = ' four ', ' value ' =>4), Array ( ' name ' = ' = ' five ', ' value ' =>5), Array (' name ' = ' = ' six ', ' value ' =>6), Array (' name ' = = ') Seven ', ' value ' =>7), Array (' name ' = ' = ' eight ', ' value ' =>8), Array (' name ' = ' = ' nine ', ' Value ' =>9) , Array (' name ' = ' ten ', ' Value ' =>10), header (' Content-type:application/json '); Echo Json_encode ($data) ;? >
This article explains the PHP JSON data using gzip compression output of the relevant content, more relevant content please focus on PHP Chinese web.