<?php
// Use Database?
$USE_DATA = false;
// Search zipcode
$zip_code = $_GET[ ($_GET['zipcode2'] != NULL) ? 'locale' : 'city' ];
// Connect to cache
$cache = new Memcache;
$cache_connected = $cache->connect('localhost', 11211);
$cache_key = 'weather_zip_'.$zip_code;
$cache_data = $cache->get($cache_key);
if ($cache_data) {
// There's a cache hit, nothing left to do.
echo $cache_data;
} else {
// Get weather.com state code
include_once( $USE_DATA ? 'database.php' : 'state_list.php' );
// Fetch raw data from weather.com
$request_url1 = 'http://wxdata.weather.com/wxdata/obs_hirad/'.$state_code.'.xml?key=47ff9b4c-91d5-11e0-b754-001c23d537c5&units=m&locale='.$_GET['locale'].'';
$xml1 = simplexml_load_file($request_url1);
$request_url2 = 'http://wxdata.weather.com/wxdata/df/'.$state_code.'.xml?key=47ff9b4c-91d5-11e0-b754-001c23d537c5&day=1,2,3,4,5,6&units=m&locale='.$_GET['locale'].'';
$xml2 = simplexml_load_file($request_url2, NULL, LIBXML_NOCDATA);
$request_url3 = 'http://wxdata.weather.com/wxdata/loc/'.$state_code.'.xml?key=47ff9b4c-91d5-11e0-b754-001c23d537c5&locale='.$_GET['locale'].'';
$xml3 = simplexml_load_file($request_url3, NULL, LIBXML_NOCDATA);
// Extract relevant data
$forecast1 = $xml2->xpath("/forecasts/forecast");
$forecast2 = $xml2->xpath("/forecasts/forecast/day");
$current = $xml1->xpath("/observation");
$name = $xml3->xpath("/location");
// Record date & time
date_default_timezone_set($name[0]->zoneInfo) ;
$fecha = $xml1->xpath("/weatherdata/weather/current");
$dia= date('d', time() + 0);
$mes= date('m', time() + 0);
$año= date('Y', time() + 0);
$Hora= date('H:i',time() + 0);
// If the cache is active, record output so we can store it
if ($cache_connected) ob_start();
// Generate the output
?>
{"weatherinfo":{
"sktq" : "<?= $forecast1[0]->maxTemp ?>\u00b0/<?= $forecast1[0]->minTemp ?>",
"temp1" : "<?= $current[0]->temp ?>\u00b0",
"temp2" : "<?= $forecast1[1]->maxTemp ?>\u00b0/<?= $forecast1[1]->minTemp ?>\u00b0",
"temp3" : "<?= $forecast1[2]->maxTemp ?>\u00b0/<?= $forecast1[2]->minTemp ?>\u00b0",
"temp4" : "<?= $forecast1[3]->maxTemp ?>\u00b0/<?= $forecast1[3]->minTemp ?>\u00b0",
"temp5" : "<?= $forecast1[4]->maxTemp ?>\u00b0/<?= $forecast1[4]->minTemp ?>\u00b0",
"temp6" : "<?= $forecast1[5]->maxTemp ?>\u00b0/<?= $forecast1[5]->minTemp ?>\u00b0",
"weather1" : "<?= $current[0]->text ?>",
"weather2" : "<?= $forecast2[1]->phrase ?>",
"weather3" : "<?= $forecast2[2]->phrase ?>",
"weather4" : "<?= $forecast2[3]->phrase ?>",
"weather5" : "<?= $forecast2[4]->phrase ?>",
"weather6" : "<?= $forecast2[5]->phrase ?>",
"wind1" : "<?= $current[0]->wSpeed ?> kph <?= $current[0]->wDirText ?>",
"wind2" : "<?= $forecast2[0]->wSpeed ?> kph <?= $forecast2[0]->wDirText ?>",
"wind3" : "<?= $forecast2[1]->wSpeed ?> kph <?= $forecast2[1]->wDirText ?>",
"wind4" : "<?= $forecast2[2]->wSpeed ?> kph <?= $forecast2[2]->wDirText ?>",
"wind5" : "<?= $forecast2[3]->wSpeed ?> kph <?= $forecast2[3]->wDirText ?>",
"wind6" : "<?= $forecast2[4]->wSpeed ?> kph <?= $forecast2[4]->wDirText ?>",
"pressure" : "<?= $current[0]->pressureDescription?>",
"uvText" : "<?= $current[0]->uvText?>",
"humid" : "<?= $current[0]->humid?>%",
"visibility": "<?= $current[0]->visibility?> km",
"cityid" : "<?php echo $_GET['city'];?>",
"city_en" : "<?= $name[0]->name ?>",
"city" : "<?= $name[0]->name ?>, <?= $name[0]['countryCode'] ?>",
"date_y" : "<?php echo"".$año;?>\u5e74<?php echo"".$mes;?>\u6708<?php echo"".$dia;?>\u65e5",
"sktime" : "<?php echo"".$Hora;?>",
"pubtime" : "<?php echo"".$año;?>-<?php echo"".$mes;?>-<?php echo"".$dia;?> <?php echo"".$Hora;?>"}}
<?php
if($cache_connected) {
// Store the output
$cache_data = ob_get_flush();
$cache->set($cache_key, $cache_data, 0, 10);
$cache->close();
}
}
?>