OK, I am working on having this code work efficiently and moving more towards the coding standards supplied in the Wiki. This is what I have so far:
Snippet:
<?php
// WEATHER.COM XML PARSER
// Version 1.4
// Copyright 2005 Nick Schaffner
// http://53x11.com
//Resource Name: WeatherX
//////// Set Varibles Below
$wxZipcode = 'XXXXX'; // Zipcode
$wxPartner = 'XXXXXXXXXX'; // Partner ID
$wxLicense = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';// License Key
$wxIcons = 'weather'; // Path to Weather Icons from Include
$wxUnits = 'english'; // Set to 'english' (Fahrenheit & MPH) or 'metric' (Celsius & KMH)
$wxClock = 12; // Set to '12' or '24' hour format
$wxUrl = 'http://xoap.weather.com/weather/local/'; // URL to xoap.weather
//////// Parse XML
$file = $wxUrl . $wxZipcode . '?cc=*&&prod=xoap&par=' . $wxPartner . '&key=' . $wxLicense;
$xml_parser = xml_parser_create();
if (file_exists($file)){
$data = implode('', file($file));
} else {
$fp = fopen($file,'r');
while(!feof($fp)){
$data = $data . fread($fp, 1024);
}
fclose($fp);
}
xml_parse_into_struct( $xml_parser, $data, $vals, $index );
xml_parser_free( $xml_parser );
$t = true;
foreach ($vals as $key => $i) {
if ($vals[$key]['value'] != false) {
if ($vals[$key]['tag'] != 'T') {
$temp = strtolower($vals[$key]['tag']);
$$temp = $vals[$key]['value'];
} elseif ($t == true) {
if ($vals[$key]["level"] == 3) $conditions = $vals[$key]['value'];
else { $windir = $vals[$key]['value']; $t = false; break; }
}
}
}
//////// Make sense of the varibles
$sunr = str_replace (' ','',$sunr);
$suns = str_replace (' ','',$suns);
$date = date("l, F jS Y @ g:ia");
if ($wxUnits == 'metric') { // Set Metric
$tmp = round((($tmp - 32)/9) * 5);
$flik = round((($flik - 32)/9) * 5);
$s = round($s * 1.609);
}
if ($wxClock == 24) {
$sunr = date("H:i",strtotime($sunr));
$suns = date("H:i",strtotime($suns));
}
if ($s == 'calm') $wind = 'Calm';
$wxIcon = $modx->config['base_url'] . $wxIcons . '/' . $icon . '.png';
$modx->setPlaceholder('wxIcon', $wxIcon);
$modx->setPlaceholder('wxSunr', $sunr);
$modx->setPlaceholder('wxSuns', $suns);
$modx->setPlaceholder('wxDate', $date);
$modx->setPlaceholder('wxWindSpeed', $s);
$modx->setPlaceholder('wxConditions', $conditions);
$modx->setPlaceholder('wxTemp', $tmp);
$modx->setPlaceholder('wxWindDir', $windir);
?>
Chunk:
[!weather!]
<div>
<p>Temp: [+wxTemp+]°F</p>
<p>Wind: [+wxWindDir+] @ [+wxWindSpeed+]mph</p>
<p>Conditions: [+wxConditions+]</p>
<p>Date: [+wxDate+]</p>
<img src="[+wxIcon+]">
</div>
This allows me to output a majority of the information I would imagine someone would want. The only problem is that the way it is setup now, the current weather will be grabbed everytime someone loads the page. This would likely cause the license from Weather.com to be terminated. To avoid this the original script was intended to be used with a Cron Job that would invoke the Script every 10 or 15 minutes to write to a file that and the the locally stored text file would be served up to the users when the load the page. Is there a way to have ModX invoke this script only if it hasn’t been invoked by any user loading the page in the last 10 to 15 minutes? Also, any suggestions on adding in/removing some of the functionality I took out of the original script?