From Marty, 2 Years ago, written in PHP.
Embed
  1. <?php
  2.  
  3. /*
  4.         Insert an aviation quote into your website
  5.         http://www.flight.org/random-aviation-quotes-on-your-wordpress-website (WordPress Plugin)
  6.         Required to create a cache directory & currentquote.txt file
  7.         Example usage: echo latestaviationquote();
  8. */
  9.  
  10.  
  11. function latestaviationquote() {
  12.  
  13.  /* Cache time & full path to cache text file */
  14.  $cache = '3600';
  15.   $cacheFile = '/full/path/to/cache/currentquote.txt';
  16.  
  17.    if ( (file_exists($cacheFile) && (time() - $cache < filemtime($cacheFile))) && (filesize($cacheFile != 0)) ) {
  18.    $result = file_get_contents("$cacheFile");
  19.    return $result;
  20.  
  21.      } else {
  22.  
  23.       $xml = simplexml_load_file("http://api.flight.org/quotes/xml/random.xml");
  24.  
  25.       if ($xml->status->attributes()->{'code'} == '200') {
  26.  
  27.         $quote = $xml->quote;
  28.         $author = $xml->quote->attributes()->{'by'};
  29.  
  30.         $return .= '&ldquo;' . $quote . '&rdquo;';
  31.         if ($author) $return .= '&nbsp;--&nbsp;' . $author;
  32.  
  33.          $fp = fopen($cacheFile, 'w');
  34.          fwrite($fp, $return);
  35.          fclose($fp);
  36.          return $return;
  37.  
  38.          } else {
  39.  
  40.          /* We'll keep the old file until the API behaves */
  41.          $result = file_get_contents("$cacheFile");
  42.          return $result;
  43.  
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. /* Usage */
  50. echo latestaviationquote();
  51. ?>