0

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

Jeremy
  • 1
  • 85
  • 340
  • 366
hally
  • 35
  • 1
  • 5
  • 1
    Maybe you should try [cURL](http://php.net/manual/en/book.curl.php) – BoltClock Jan 21 '11 at 16:50
  • 3
    I cannot confirm this. `` gives me JSON. – Oswald Jan 21 '11 at 16:54
  • Instead of the questionable curl advise, you should try PEAR Http_Request or Zend_Http, which provides a sensible API. Include a correct `Accept:` header. – mario Jan 21 '11 at 17:01
  • @mario Out of interest, why would you recommend a PEAR extension over cURL? – John Parker Jan 21 '11 at 17:04
  • @middaparka: It's made for HTTP (unlike curl which everybody mistakes for a http client), provides a non-retarded API, does not depend on installed pecl extensions. – mario Jan 21 '11 at 17:06
  • @mario Ta for the info - it's always interesting to hear such things. :-) – John Parker Jan 21 '11 at 17:09
  • The method i was using to substitute $COMIC_NUM into the url messed up the actual url, and so, when using file_get_contents, with a bad url, i was just being redirected to the comic page.Thanks, it's working perfectly now. – hally Jan 21 '11 at 17:12

2 Answers2

2

Whilst I it personally works perfectly with file_get_contents for me, you could try using cURL as follows (if you have it available) as this would be a more robust solution:

<?php
    $COMIC_NUM = 849;

    $curlSession = curl_init();
    curl_setopt($curlSession, CURLOPT_URL, '"http://xkcd.com/'.$COMIC_NUM.'/info.0.json');
    curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
    $jsonData = curl_exec($curlSession);
    curl_close($curlSession);

    echo($jsonData);
?>
John Parker
  • 54,048
  • 11
  • 129
  • 129
0

Works fine for me. Maybe $COMIC_NUM isn't set properly?

php > echo file_get_contents('http://xkcd.com/847/info.0.json');
{"img": "http://imgs.xkcd.com/comics/stingray_nebula.png", "title": "Stingray Nebula", "month": "1", "num": 847, "link": "", "year": "2011", "news": "", "safe_title": "Stingray Nebula", "transcript": "[[Two white figures are silhouetted against a dark sky.  They're sitting on top of a grassy hill.]]\nPerson: I know things are tough right now.  When I was going through some difficult times as a kid, I would go up on the roof and look through my telescope.\n\nPerson: One day I found a tiny star in Ara that seemed friendly.\nPerson: There were millions like it, but I decided that this one was mine.\n\nPerson: When things got bad, I'd go find that star, and think of my favorite Tolkien quote.  It's from Sam's time in Mordor.\n\n((The next panel is diagonally downward to the right of the previous.  The upper left corner overlaps.))\n[[A star is above the highest peak in a chain of mountains.]]\n\"There, peeping among the cloud-wrack above a dark tor high up in the mountains, Sam saw a white star twinkle for a while.  The beauty of it smote his heart, as he looked up out of the forsaken land, and hope returned to him.  For like a shaft, clear and cold, the thought pierced him that in the end the shadow was only a small and passing thing: There was light and high beauty forever beyond its reach.\"\n- The Return of the King\n\nCompanion: That's comforting!\nPerson: It was rather undercut in 1987, when the light from my star's explosion reached Earth.  The debris forms the Stingray Nebula.\n\nCompanion: There's probably a lesson there.\nPerson: \"Never trust an unstable asymptotic giant branch star.  Stick with main sequences and dwarfs.\"\nCompanion: I'll, uh, keep that in mind.\n\n{{Title text: E\u00c3\u00a4rendil will patrol the walls of night only until the sun reaches red giant stage, engulfing the Morning Star on his brow. Light and high beauty are passing things as well.}}", "alt": "E\u00c3\u00a4rendil will patrol the walls of night only until the sun reaches red giant stage, engulfing the Morning Star on his brow. Light and high beauty are passing things as well.", "day": "14"}
mfonda
  • 7,873
  • 1
  • 26
  • 30