Page 1 of 1

Wunderground API calls

Posted: Tue 23 Aug 2016 11:58 pm
by Hunter362
I'm including a .php script in my html index page. I've set the cache time to 4 hours, shouldn't the script look at the cached file for the info rather than running the script again? It's getting the temperature records for today's date, so it really only needs to be called once or twice during the day right?
Found the script in the wxfourms, I believe Ken True wrote it or modified it to work for a user.

Or just a way to get the associated values to insert as the webtags and just call the script once via a cron job


I get the API usage alert from wunderground, about going over the 500 calls/day

Your wunderground API key (xxx) exceeded its allotted usage today by
making 503 calls in a day but the limit is 500.

Code: Select all

<?php
// Settings:
$APIkey = 'xxxx';

$WU_URL = 'http://api.wunderground.com/api/'.$APIkey.'/almanac/q/NY/Syracuse.json';

$cacheFileDir = './cache/';                     // default cache file directory
$cacheName = "WU-almanac-Syr.txt";           // locally cached page from WU
// $refetchSeconds = 1800;                    // cache lifetime (1800sec = 30 minutes)
$refetchSeconds = 14400;

// end of settings
// overrides from Settings.php if available
global $SITE;
if(isset($SITE['cacheFileDir']))     {$cacheFileDir = $SITE['cacheFileDir']; }
// end of overrides from Settings.php
//
// -------------------begin code ------------------------------------------
$Status = "<!-- begin WU-record via API -->\n";
$Force = false;
if (isset($_REQUEST['force']) and  $_REQUEST['force']=="1" ) {
  $Force = true;
}

$doDebug = false;
if (isset($_REQUEST['debug']) and strtolower($_REQUEST['debug'])=='y' ) {
  $doDebug = true;
}

$fileName = $WU_URL;
if ($doDebug) {
  $Status .= "<!-- WU URL: $fileName -->\n";
}

$cacheName = $cacheFileDir . $cacheName;

// The number 1800 below is the number of seconds the cache will be used instead of pulling a new file
// 1800 = 60s x 30m so it retreives every 30 minutes. 

if (! $Force and file_exists($cacheName) and filemtime($cacheName) + $refetchSeconds > time()) {
      $html = implode('', file($cacheName)); 
      $Status .= "<!-- loading from $cacheName (" . strlen($html) . " bytes) -->\n"; 
  } else { 
      $Status .= "<!-- loading from $fileName. -->\n"; 
      $html = fetchUrlWithoutHangingWUR($fileName,true); 
      $fp = fopen($cacheName, "w"); 
	  if (!$fp) { 
	    $Status .= "<!-- unable to open $cacheName for writing. -->\n"; 
	  } else {
        $write = fputs($fp, $html); 
        fclose($fp);  
		$Status .= "<!-- saved cache to $cacheName (". strlen($html) . " bytes) -->\n";
	  } 
} 

  if($doDebug) { print $Status; }

  $JSON = json_decode($html,true); // get as associative array
  $recordHigh = $JSON['almanac']['temp_high']['record']['F'];
  $recordHighYear = $JSON['almanac']['temp_high']['recordyear'];
  $recordLow = $JSON['almanac']['temp_low']['record']['F'];
  $recordLowYear = $JSON['almanac']['temp_low']['recordyear'];
  $normalHigh = $JSON['almanac']['temp_high']['normal']['F'];
  $normalLow = $JSON['almanac']['temp_low']['normal']['F'];

// put rest of your processing here

// Functions --------------------------------------------------------------------------------

function WURmicrotime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}
// ------------------------------------------------------------------

// get contents from one URL and return as string 
 function fetchUrlWithoutHangingWUR($url,$useFopen) {

  global $Status, $needCookie,$timeStamp,$TOTALtime;
  $overall_start = time();
 
//   print "<!-- using file function -->\n";
   $T_start = WURmicrotime_float();

   $xml = implode('',file($url));
   $T_close = WURmicrotime_float();
   $ms_total = sprintf("%01.3f",round($T_close - $T_start,3)); 
   $Status .= "<!-- file() stats: total=$ms_total secs -->\n";
   $TOTALtime+= ($T_close - $T_start);
   $overall_end = time();
   $overall_elapsed =   $overall_end - $overall_start;
   $Status .= "<!-- fetch function elapsed= $overall_elapsed secs. -->\n"; 
   return($xml);

   }    // end fetchUrlWithoutHangingWUR
// ------------------------------------------------------------------
?>

Re: Wunderground API calls

Posted: Wed 24 Aug 2016 6:40 am
by weatherist34
hi

do you have any other scripts using the same api key ??