Why does php's file_get_contents incorrectly retrieve json files from xkcd?

Posted by hally on Stack Overflow See other posts from Stack Overflow or by hally
Published on 2011-01-21T16:47:09Z Indexed on 2012/07/08 3:16 UTC
Read the original article Hit count: 116

In writing a PHP script to download xkcd comics, i incurred errors when trying to get specific comics (as opposed to the latest one). Specifically, pointing file_get_contents at the following url:

xkcd.com/$COMIC_NUM/info.0.json

inexplicably retrieved the xhtml version of the comic's page on xkcd.com, and not a JSON file. However, if i request the exact same url in my browser, the correct JSON file is downloaded. I'm not sure why this is happening, but i suspect it has something to do with the request headers being sent.

Please help! :S

© Stack Overflow or respective owner

Related posts about php

Related posts about JSON