How to use gzip to compress and output JSON data returned by the PHP program _ PHP

Source: Internet
Author: User
This article mainly introduces how to use gzip to compress and output JSON data returned by PHP. the sample environment in this article is Linux and Apache server. if you need it, refer 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.