Use gzip to compress the JSON format data returned by the PHP program.
1. Comparison between HTTP output with and without compression
2. Enable gzip
Use apache mod_deflate module to enable gzip
Enabling method:
sudo a2enmod deflatesudo /etc/init.d/apache2 restart
Close method:
sudo a2dismod deflatesudo /etc/init.d/apache2 restart
3. Set the type of gzip compression output
The json output type is application/json.
In <Directory> </Directory> of httpd. conf, add
<IfModule mod_deflate.c>AddOutputFilterByType DEFLATE application/json</IfModule>
<?php$data = array( array('name'=>'one','value'=>1), array('name'=>'two','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);?>
Set the output before gzip:
Output after setting gzip:
4. Use gzip to compress the output of a single json File
After AddOutputFilterByType DEFLATE application/json is set, gzip is used to compress all data output in json format.
If you only want to use gzip to compress the output of a json file, you can use ob_start.
First, you do not need to set AddOutputFilterByType, and then add ob_start ('ob _ gzhandler') at the beginning of the Code ');
<?phpob_start('ob_gzhandler');$data = array( array('name'=>'one','value'=>1), array('name'=>'two','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);?>
Articles you may be interested in:
- JQuery sends a request to the PHP server through Ajax and returns JSON data
- Php simply queries the json data returned by the database
- Share classes returned by JSON and XML in PHP
- ThinkPHP returns JSON using AJAX.
- Php json_encode () function returns the json data instance code
- Php return json data function instance
- Solution for PHP to return NULL when processing Json string Decoding
- Example code for processing json data returned by php using ajax