Skip to content

Instantly share code, notes, and snippets.

@Yirba
Created August 2, 2012 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yirba/3239432 to your computer and use it in GitHub Desktop.
Save Yirba/3239432 to your computer and use it in GitHub Desktop.
xkcdInfo
<?php
/*************************************************
** xkcdInfo for explainxkcd.com **
** Version: 1.0.0 **
** Developed by Yirba <yirba3 AT gmail DOT com> **
** Created: 2 August 2012 **
** Last modified: 2 August 2012 **
*************************************************/
//get data
$data = $_GET['input'];
//get user input
$input = explode(":", $data);
//separate variables
$var = explode("-", $input[1]);
//retrieve xkcd JSON
if ($var[0] == 0) {
$json = file_get_contents("http://xkcd.com/info.0.json");
}
else {
$json = file_get_contents("http://xkcd.com/$var[0]/info.0.json");
}
//decode JSON
$decoded = json_decode($json, true);
//check if user wants to embed the comic image
if ($var[1] == "embed") {
//output image
echo '<img src="' , $decoded['img'] , '" title="' , $decoded['alt'] , '" alt="' , $decoded['safe_title'] , '">';
}
else {
//output decoded JSON
echo $decoded[$var[1]];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment