0

BTW, this is not a duplicate, I'm not trying to compress in PHP, I'm trying to compress Client Side and uncompress in PHP.

I'm trying to compress a JSON array containing 5 base64 images and some text to my PHP api.

I have tried lz-string and pako and it appears to be sending the compressed payload as expected but I'm having issues decompressing on the PHP backend.

Typescript Gzip.

  var payload = pako.gzip(JSON.stringify(data), { to: 'string' });

  let headers = new Headers({ "Content-Encoding" : "gzip"});
  headers.append('Content-Type', 'application/json; charset=x-user-defined-binary');
  let requestOptions = new RequestOptions({ headers: headers });

  var url = this.baseUrl + "/app/notice/issue/compressed";
  return this.http.post(url, data, requestOptions).pipe(timeout(100000), map(res => { return res.json() }), catchError(err => { return throwError(err.message); }));

PHP

$input = file_get_contents("php://input");
$test = gzdecode($input);
echo $test;

Output

"

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  gzdecode(): data error</p>


</div>"

If anyone can help in reducing the size of my JSON, that would be really helpful.

Thanks.

Dunny
  • 87
  • 1
  • 1
  • 13
  • refer to this, though it is java will point you in the right direction https://stackoverflow.com/a/1407637/5956589 also check out the other others referring to the php uncompressing part – Richard Muvirimi Jun 09 '20 at 17:19

1 Answers1

0

You PHP exapmle looks fine and should work.

I'm not sure about the Typescript part: binary string can be additionaly encoded (as UTF-8 maybe?) before being sent by browser or http.post() method.

Check sent Content-Length of the request and actual length of the binary string after compression. They obviously must be the same.

I suggest removing {to: 'string'} option in pako.gzip() method to get Uint8Array instead of string and use XMLHttpRequest instead of http.post()

See also:

alex
  • 51
  • 4
  • oh yeah sorry, typo in the question, but yes it's being sent. Lots of weird characters showing in the inspector headers. – Dunny Jul 29 '19 at 15:13
  • ok, i changed my answer, you can edit the question to fix the typo. – alex Jul 30 '19 at 11:02