Skip to content
Advertisement

Decoding a gzipped string in PHP

I am trying to decode a gzipped body of a REST response (YouTrack-API) with PHP. Nothing seems to work:

  • Automatic decoding from CURL does nothing
  • Using Guzzle with headers set to accept encoding does nothing
  • Simply putting the acquired string into gzdecode() does nothing

I have tried gzinflate, gzdecode and some wild combinations of them including stripping off bytes at the start and end, but without success. I am by no means an expert in compression, so I have no idea if there are even different formats a gzipped string can take, but any old ‘online gzip service’ can decode the string with no problem at all

Simple example: This string:

H4sIAAAAAAAA//NIzcnJV8jPSVEozy/KSQEARAYhbw8AAAA=

should output:

Hello old world

Put into any online converter I could find, it works, put into any of the PHP functions: data error. What is the deal with PHP and gzip not working at all? has it something to do with either the entire response being gzipped or just the contents?

Advertisement

Answer

The gzipped string is base64 encoded, so you need to do:

echo gzdecode(base64_decode('H4sIAAAAAAAA//NIzcnJV8jPSVEozy/KSQEARAYhbw8AAAA='));
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement