<?php require_once 'WeatherForecast.php'; require_once 'WeatherDayInfo.php'; $url = "http://jornaldotempo.uol.com.br/rss/cidades/rss_prev_Lavras-MG.xml"; $reader = new RSSReader(simplexml_load_file($url)); $rssItem = $reader->getRssWeahter(); $wheaterForecast = new WeatherForecast($rssItem->title(), $rssItem->description()); $daysInfo = $wheaterForecast->daysInfo(); ?> <div id="weatherforecast"> <label for="weather-city" style="color: #921b1b; font-weight: bold;"> Para trocar a cidade clique na caixa abaixo </label><br /> <h2> <!--<span><?echo $wheaterForecast->city()?></span>--> <?include "cities.html"?> </h2> <div class="painel-weather"> <table class="weather-table" border="0"> <tbody> <?$i = 0;?> <?while($daysInfo->valid() && $i < 4):?> <?$dayInfo = $daysInfo->current();?> <?if($i % 2 == 0):?> <tr> <?endif?> <td> <div class="td-weather"> <?echo $dayInfo->day()?> <!--(<?//echo $dayInfo->situation()?>)--> <br/> <div class="weather-info">
/** * Gets the most current weather forecast for Cuba * * @param Request * @return Response * */ public function _main(Request $request) { // include the weather channel library include_once "{$this->pathToService}/lib/WeatherForecast.php"; $argument = $request->query; $argument = trim($argument); $argument = trim(strtolower($argument)); $weather = array(); $images = array(); $imagesya = array(); $places_cuba = explode(",", "La Habana,Pinar del Rio,Artemisa,Batabano,Varadero,Matanzas,Santa Clara,Cienfuegos,Sancti Spiritus,Trinidad,Camaguey,Ciego de Avila,Las Tunas,Holguin,Bayamo,Santiago de Cuba,Guantanamo"); $places = $places_cuba; $country = 'Cuba'; if (trim($argument) != '') { $arr = explode(',', $argument); $arr[0] = trim($arr[0]); if (isset($arr[1])) { $arr[1] = trim($arr[1]); } else { $arr[1] = ''; } $city = $arr[0]; $country = $arr[1]; if ("{$country}" == '') { $country = false; } if ("{$city}" == '' && "{$country}" != '') { $city = $country; $country = false; } $places = array($city); } if ($country != 'Cuba') { $r = new WeatherForecast('93fvz526zx8uu26b59cpy9xf'); $x = $r->setRequest($places[0], $country, 3); if ($x === false) { $places = $places_cuba; $country = 'Cuba'; } } // get the weather information for each province foreach ($places as $place) { // get the weather forecast $r = new WeatherForecast('93fvz526zx8uu26b59cpy9xf'); $r->setRequest($place, $country, 3); $r->setUSMetric(false); $r = $r->getLocalWeather(); if (!$r) { continue; } // get weather details for today $today = new stdClass(); $today->location = $place; $today->time = $r->weather_now['weatherTime']; $today->temperature = $r->weather_now['weatherTemp']; $today->description = $this->getDescriptionBasedOnCode($r->weather_now['weatherCode']); $today->icon = $this->getImageBasedOnCode($r->weather_now['weatherCode']); if (!isset($imagesya[$today->icon])) { $imagesya[$today->icon] = true; $images[] = $today->icon; } $today->windDirection = $r->weather_now['windDirection']; $today->windSpeed = $r->weather_now['windSpeed']; $today->precipitation = $r->weather_now['precipitation']; $today->humidity = $r->weather_now['humidity']; $today->visibility = $r->weather_now['visibility']; $today->pressure = $r->weather_now['pressure']; $today->cloudcover = $r->weather_now['cloudcover']; // get weather details for next 3 days $days = array(); foreach ($r->weather_forecast as $w) { $day = new stdClass(); $day->date = $w['weatherDate']; $day->weekday = $this->translate($w['weatherDay']); $day->description = $this->getDescriptionBasedOnCode($w['weatherCode']); $day->icon = $this->getImageBasedOnCode($w['weatherCode']); if (!isset($imagesya[$day->icon])) { $imagesya[$day->icon] = true; $images[] = $day->icon; } $day->windDirection = $w['windDirection']; $day->windSpeed = $w['windSpeed']; $day->tempMax = $w['tempMax']; $day->tempMin = $w['tempMin']; $days[] = $day; } // add days to the final result $today->days = $days; $weather[] = $today; } $response = new Response(); $response->setResponseSubject("El Clima"); $response->createFromTemplate("basic.tpl", array("weather" => $weather), $images); return $response; }
<?php require_once 'WeatherForecast.php'; require_once 'WeatherDayInfo.php'; $url = "http://jornaldotempo.uol.com.br/rss/cidades/rss_prev_" . $_GET['id'] . ".xml"; $reader = new RSSReader(simplexml_load_file($url)); $rssItem = $reader->getRssWeahter(); $wheaterForecast = new WeatherForecast($rssItem->title(), $rssItem->description()); $daysInfo = $wheaterForecast->daysInfo(); ?> <table class="weather-table" border="0"> <tbody> <?$i = 0;?> <?while($daysInfo->valid() && $i < 4):?> <?$dayInfo = $daysInfo->current();?> <?if($i % 2 == 0):?> <tr> <?endif?> <td> <div class="td-weather"> <?echo $dayInfo->day()?> <!--(<?//echo $dayInfo->situation()?>)--> <br/> <div class="weather-info"> <div class="img-weather"> <?echo str_replace("src='imgs", "src='".LinkController::getBaseURL()."/images/weather_imgs", $dayInfo->imageSituation())?> </div> <div class="weather-data"> <img alt="Mínima" src="<?echo LinkController::getBaseURL()?>/images/weather_imgs/cli_min.gif"/> Mín.: <?echo $dayInfo->min()?><br /> <img alt="Mínima" src="<?echo LinkController::getBaseURL()?>/images/weather_imgs/cli_max.gif"/> Max.: <?echo $dayInfo->max()?> </div> </div> </div>
/** * Returns data from the weather * * @param string $type_of_request Type of the URL of the request * @return SimpleXMLIterator Object */ private function getRequest($type_of_request = 'local') { // Gets the unique key of the API $key = self::getKey(); if (empty($key)) { echo 'The API key is missing.'; return false; } // Gets the City name $city = self::$city; // Gets the Country name $country = self::$country ? ',' . self::$country : null; // Gets the Number of days to display $num_of_days = self::$num_of_days; // Checks if has been set the city if (empty($city)) { echo 'You can specify the location for any of these options: City and Town Name, IP Address, UK Postcode, Canada Postal Code, US Zipcode and Latitude and Longitude (in decimal).'; return false; } // Stores the request URL $request = self::$request[$type_of_request] . '?q=' . $city . $country . '&format=xml&num_of_days=' . $num_of_days . '&key=' . $key; // Lifetime of the cache file, in seconds (1 hour = 3600) $cache_life = '0'; // Cache file $cache_file = '../temp/cached-' . (substr($key, 0, 8) . '-' . $city . $country) . '.xml'; // Generates the cache file if it does not exist or has expired life time $filemtime = @filemtime($cache_file); if (!$filemtime || time() - $filemtime >= $cache_life) { // Starts the request to cache $contents = @file_get_contents($request); // Reads the request and checks for errors $doc = simplexml_load_string($contents); // Does not make the file cache on error if (isset($doc->error)) { $doc_error = $doc->error; $msg = !empty($doc_error->msg) ? $doc_error->msg : 'Data could not be retrieved.'; if (self::$display_error) { echo '<strong style="color:red;">' . $msg . '</strong>'; return false; } } else { file_put_contents($cache_file, $contents); } } // Initiates the request if (filesize($cache_file) > 0) { $doc = simplexml_load_file($cache_file); self::$has_response = true; return self::$doc = $doc; } else { return null; } }