PHP JSON data uses gzip to compress output related content

Source: Internet
Author: User
Tags php json

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.

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.