php-ext-brotli icon indicating copy to clipboard operation
php-ext-brotli copied to clipboard

Using php-ext-brotli to encode data for HTTP to browser

Open rhclayto opened this issue 7 years ago • 1 comments

Hi,

I'm not sure if this is an issue with this extension at all, but thought I'd make an issue here in case it's anything that anyone here knows about.

I'm trying to encode some JSON data to serve out of a ReactPHP app to browsers. In Postman, I access the JSON via HTTPS, with Accept-Encoding: br, & get Content-Encoding; br back. But the body simply says: Unexpected '*'.

The code I am using to compress the data:

if (!empty($requestHeaders['Accept-Encoding'])) {
        $encodings = array_map('trim', explode(',', $requestHeaders['Accept-Encoding'][0]));
        if (function_exists('brotli_compress') && in_array('br', $encodings)) {
          $returnContent = brotli_compress($returnContent, 5, BROTLI_TEXT);
          $output_headers['Content-Encoding'] = 'br';
          $output_headers['Content-Length'] = strlen($returnContent);
        }
        elseif (in_array('deflate', $encodings)) {
          $returnContent = gzcompress($returnContent);
          $output_headers['Content-Encoding'] = 'deflate';
          $output_headers['Content-Length'] = strlen($returnContent);
        }
        elseif (in_array('gzip', $encodings)) {
          $returnContent = gzencode($returnContent);
          $output_headers['Content-Encoding'] = 'gzip';
          $output_headers['Content-Length'] = strlen($returnContent);
        }
      }

GZIP & ZLIB (deflate) work fine, brotli gives errors. Any ideas?

rhclayto avatar Mar 17 '17 12:03 rhclayto

Could you try brotli_compress($returnContent, 1, BROTLI_GENERIC); ?

sergey-dryabzhinsky avatar Jan 10 '18 10:01 sergey-dryabzhinsky