Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rhclayto opened this issue Mar 17, 2017 · 1 comment
Open

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

rhclayto opened this issue Mar 17, 2017 · 1 comment

Comments

@rhclayto
Copy link

rhclayto commented Mar 17, 2017

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?

@sergey-dryabzhinsky
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants