OpenSwoole\Http\Response->gzip()

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\Http\Response->gzip(int $level = 1)

Parameters

level

The HTTP GZIP compression level (1-9)

Return

successful

No value is returned

fails

If setting the header fails, then false will be returned

Description

This method has been deprecated.

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.

Requirements

This method requires the zlib library. Install zlib by running sudo apt-get install libz-dev.

Example

<?php

$server->on('Request', function(OpenSwoole\Server\Request $request, OpenSwoole\Server\Response $response)
{
  $response->gzip(2);

  $response->end('Hello World!');
});
Last updated on September 20, 2022