How to Use gzip to compress the JSON format data returned by the PHP Program

Source: Internet
Author: User
1. Compare HTTP output with HTTP output without compression 2. Enable gzip by using apachemod_deflatemodule. Enable gzip. Disable. dapache2restart: Disable. dapache2restart3. Set gzip compression output

1. use compression and HTTP output without compression 2. how to enable gzip using apache mod_deflate module: sudo a2enmod deflatesudo/etc/init. d/apache2 restart: sudo a2dismod deflatesudo/etc/init. d/apache2 restart 3. set gzip compression output

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 httpd. conf Join

 
  AddOutputFilterByType DEFLATE application/json
 

<?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);?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.