Skip to content
Advertisement

how to decompress data in php and the data which use pako.deflate from js

I use pako.deflate to compress data in javascript, like this:

js file:

const params = [{id: 5, name: '张三', list: [{code: '10010', type: 'media'}]},{id: 6, name: '李四', list: [{code: '20010', type: 'site'}]}]

let binaryString = pako.deflate(JSON.stringify(params), { to: 'string' })

http.post({data: binaryString})...

and in the web server, I need to decompress that data using PHP.

This is what I do

php file:

$data = $params['data']; // got the right post data

$res = gzinflate(base64_decode($data));

echo $res; //echo false

but $res echo false

What am I missing?

Advertisement

Answer

You are base 64 decoding at the server – do you actually base64 encode at the client at all prior to posting the data? There is no indication this is occurring in the code posted.

I suspect it is likely you are sending a deflated utf-8 string as your data, and then trying to base64 decode a string which contains a far greater range of characters.

Maybe look what characters $params['data'] contains – if any of them are outside the base64 range (being a-z,A-Z, 0-9, +, /, and possibly trailing =) then I think this would be the problem.

You could then try simply changing the line to:

$res = gzinflate($data);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement