PHP code example of christoph-kluge / reactphp-http-response-compression-middleware

1. Go to this page and download the library: Download christoph-kluge/reactphp-http-response-compression-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

christoph-kluge / reactphp-http-response-compression-middleware example snippets


$server = new Server(new MiddlewareRunner([
    new ResponseCompressionMiddleware([
        new CompressionGzipHandler(),
        new CompressionDeflateHandler(),
    ]),
    function (ServerRequestInterface $request, callable $next) {
        return new Response(200, ['Content-Type' => 'application/json'], json_encode([
            'some' => 'nice',
            'json' => 'values',
        ]));
    },
]));

// DefaultRegexDetector.php -> used in __construct()

new RegexDetector([
    '/^text\/[a-z-\+]+$/', // text/*
    '/^application\/json$/', // application/json
    '/^application\/xml$/', // application/xml
    '/^[a-z-\+]+\/[a-z-\+]+\+json$/', // */*+json
    '/^[a-z-\+]+\/[a-z-\+]+\+xml$/', // */*+xml
]);

new CompressionGzipHandler(new ArrayDetector[
    'text/html',
]),
new CompressionGzipHandler(new RegexDetector[
    '/^text\/[a-z]+$/',
]),

new CompressionGzipHandler(new class implements MimeDetectorInterface {
    public function isCompressible($mime) {
        // your custom magic here..
        return true;
    }
}),