Monday, March 16, 2015

In case you're looking for the solution and you don't care about the coding, it is in bold red.

Recently, I wanted to use StackExchange API where I wanted to extract some data for some purposes. I decided to use the file_get_contents method. But, surprisingly, all I could get was gibberish content with weird characters.

So, I looked everywhere on my server and on the internet and I couldn't find a solution. It turns out the lines above you see aren't gibberish, they are gzip encoded data. The proof was simple, just try to run this code

We you use a famous, overused PHP function like file_get_contents() to fetch a remote webpage, it doesn't send any http headers by default! The remote server's job is to check if you (the requester of the data, aka your server) supports compression over http (known as HTTP_ACCEPT_ENCODING).

Now it is very likely that no headers were sent with your request, the server shouldn't have replied with gzip encoded data, but they are doing so. So, it is StackExchange API fault a bit.

Anyhow, you got your solution now, let me know in the comments if you still have a problem!