Home  >  Article  >  Backend Development  >  Detailed explanation of how PHP compresses the returned JSON data and outputs it with gzip

Detailed explanation of how PHP compresses the returned JSON data and outputs it with gzip

*文
*文Original
2018-01-05 17:56:063329browse

How does PHP compress and output the returned JSON data with gzip? This article mainly introduces the method of using gzip compression to output the JSON format data returned in PHP. The example environment in the article is the Linux system and Apache server. Friends in need can refer to it. I hope to be helpful.

1. Comparison of HTTP output with compression and without compression

201633154618959.jpg (447×226)

2. Turn on gzip

Use apache mod_deflate module to enable gzip
Open method:

sudo a2enmod deflate
sudo /etc/init.d/apache2 restart

Close method:

sudo a2dismod deflate
sudo /etc/init.d/apache2 restart

3. Set the type that requires gzip compression output

The output type of json is application/json, so you can set it like this
Add

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/json
</IfModule>
<?php
$data = array(
  array(&#39;name&#39;=>&#39;one&#39;,&#39;value&#39;=>1),
  array(&#39;name&#39;=>&#39;two&#39;,&#39;value&#39;=>2),
  array(&#39;name&#39;=>&#39;three&#39;,&#39;value&#39;=>3),
  array(&#39;name&#39;=>&#39;four&#39;,&#39;value&#39;=>4),
  array(&#39;name&#39;=>&#39;five&#39;,&#39;value&#39;=>5),
  array(&#39;name&#39;=>&#39;six&#39;,&#39;value&#39;=>6),
  array(&#39;name&#39;=>&#39;seven&#39;,&#39;value&#39;=>7),
  array(&#39;name&#39;=>&#39;eight&#39;,&#39;value&#39;=>8),
  array(&#39;name&#39;=>&#39;nine&#39;,&#39;value&#39;=>9),
  array(&#39;name&#39;=>&#39;ten&#39;,&#39;value&#39;=>10),
);

header(&#39;content-type:application/json&#39;);
echo json_encode($data);
?>

before setting gzip in 7b799fe73e35dcfdc019b13f54de80e5bb15ed4aadeed04b3991578461de0768 in httpd.conf Output:

201633154714151.jpg (346×83)

After setting gzip, output:

201633154732511.jpg (334×99)

4. Single json Use gzip compressed output

After setting AddOutputFilterByType DEFLATE application/json, all data output in json format will be output using gzip compression.
If you only want to use gzip compression for a certain json and do not need the others, you can use the ob_start(); method to achieve this.

There is no need to set AddOutputFilterByType first, and then add ob_start('ob_gzhandler');

<?php
ob_start(&#39;ob_gzhandler&#39;);

$data = array(
  array(&#39;name&#39;=>&#39;one&#39;,&#39;value&#39;=>1),
  array(&#39;name&#39;=>&#39;two&#39;,&#39;value&#39;=>2),
  array(&#39;name&#39;=>&#39;three&#39;,&#39;value&#39;=>3),
  array(&#39;name&#39;=>&#39;four&#39;,&#39;value&#39;=>4),
  array(&#39;name&#39;=>&#39;five&#39;,&#39;value&#39;=>5),
  array(&#39;name&#39;=>&#39;six&#39;,&#39;value&#39;=>6),
  array(&#39;name&#39;=>&#39;seven&#39;,&#39;value&#39;=>7),
  array(&#39;name&#39;=>&#39;eight&#39;,&#39;value&#39;=>8),
  array(&#39;name&#39;=>&#39;nine&#39;,&#39;value&#39;=>9),
  array(&#39;name&#39;=>&#39;ten&#39;,&#39;value&#39;=>10),
);

header(&#39;content-type:application/json&#39;);
echo json_encode($data);
?>

Related recommendations:

PHP array traversal foreach syntax structure and examples

PHP array sorting method summary

A brief analysis of json transmission in php and js

The above is the detailed content of Detailed explanation of how PHP compresses the returned JSON data and outputs it with gzip. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn