public
Created

xkcdInfo

  • Download Gist
xkcdinfo.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?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]];
 
}
 
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.