Method of compressing output of JSON format data returned in PHP program with gzip, jsongzip_php tutorial

Source: Internet
Author: User
Tags php server

The JSON-formatted data returned in the PHP program is compressed with gzip output method, Jsongzip


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

 
  
   
  Addoutputfilterbytype DEFLATE Application/json
 
  
<?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 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 (' 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);? >

Articles you may be interested in:

    • jquery sends requests to the PHP server via AJAX and returns JSON data
    • PHP Simple implementation Query database return JSON data
    • PHP implementation returns JSON and XML class sharing
    • Thinkphp two implementations of JSON return via Ajax
    • The PHP json_encode () function returns the JSON data instance code
    • PHP returns JSON data function instance
    • Workaround for PHP to handle JSON string decoding return null
    • Ajax handles PHP instance code that returns JSON data

http://www.bkjia.com/PHPjc/1106125.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106125.html techarticle The JSON-formatted data returned in the PHP program is compressed with gzip output method, Jsongzip 1. Use compression compared to HTTP output that does not use compression 2. Turn on gzip using Apache Mod_deflate module to open ...

  • 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.