Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Http\Response->gzip(int $level = 1)
The HTTP GZIP compression level (1-9)
No value is returned
If setting the header fails, then false
will be returned
Deprecation Message: This method since v4.1.0
has been abandoned, the new http_compression
server configuration option replaces the gzip
method. The main reason is that the gzip()
method does not determine if the browser/client supports Accept-Encoding
, if the client does not support gzip
compression, it will lead to the client not being able to extract the request. The new http_compression
configuration item is based on the client Accept-Encoding
header, it will automatically select whether to compress (if the client supports it), and automatically selects the best compression algorithm.
Enable the use of gzip
HTTP compression. The server response will compress and reduce the content size, effectively saving network bandwidth and improving response times.
The Content-Encoding
header will be added automatically if you use this method.
The higher the gzip
level the smaller the content size and more compressed the response will be but this increases the CPU usage.
This method must be called before the
$response->write/end
methods.
This method requires the zlib
library. Install zlib
by running sudo apt-get install libz-dev
.
<?php
$server->on('Request', function(OpenSwoole\Server\Request $request, OpenSwoole\Server\Response $response)
{
$response->gzip(2);
$response->end('Hello World!');
});