Skip to content

Instantly share code, notes, and snippets.

@rbarros
Created June 8, 2012 12:56
Show Gist options
  • Save rbarros/2895462 to your computer and use it in GitHub Desktop.
Save rbarros/2895462 to your computer and use it in GitHub Desktop.
Classe para mostrar previsão do tempo.
<?php
/**
* Class Weather
*
* An open source application
*
* @package Weather
* @category Classes
* @author Ramon Barros
* @copyright Copyright (c) 2012, Ezoom Ltda.
* @license http://www.ramon-barros.com/
* @link http://public.ramon-barros.com/
* @since Version 0.6.12
* @filesource Weather.php
*/
class Weather {
private $url;
private $urlApi;
private $city;
private $state;
private $country;
private $language;
private $dateformat;
private $info;
private $current;
private $next;
public $day_of_week;
public $week;
/**
* Constroi o objeto que representa o tempo e salva as configurações.
* @access public
* @param string $city
* @param string $state
* @param string $country
* @param string $language
*/
public function __construct($city='Caxias do Sul', $state='Rio Grande do Sul',$country='Brasil',$language='pt-br'){
$this->city = $city;
$this->state = $state;
$this->country = $country;
$this->language = $language;
$this->dateformat = 'd/m/Y';
$this->url = 'http://www.google.com/';
$this->urlApi = rtrim($this->url,'/').'/ig/api';
}
/**
* Carrega o arquivo XML.
* @access private
* @param string $url
* @throws Exception
*/
private function getFile($url){
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
} catch (Exception $e) {
throw new Exception("Error Processing Request".$e->getMessage(), 1);
}
}
/**
* Remove acentos.
* @access private
* @param string $string
* @return string
*/
private function caracter($string){
$string = strtr($string,
"\xA1\xAA\xBA\xBF\xC0\xC1\xC2\xC3\xC5\xC7
\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1
\xD2\xD3\xD4\xD5\xD8\xD9\xDA\xDB\xDD\xE0
\xE1\xE2\xE3\xE5\xE7\xE8\xE9\xEA\xEB\xEC
\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF8
\xF9\xFA\xFB\xFD\xFF\x20\x2D",
"!ao?AAAAAC
EEEEIIIIDN
OOOOOUUUYa
aaaaceeeei
iiidnooooo
uuuyy__");
$string = strtr($string, array("\xC4"=>"Ae", "\xC6"=>"AE", "\xD6"=>"Oe", "\xDC"=>"Ue", "\xDE"=>"TH", "\xDF"=>"ss", "\xE4"=>"ae", "\xE6"=>"ae", "\xF6"=>"oe", "\xFC"=>"ue", "\xFE"=>"th"));
return strtolower( $string );
}
/**
* Le o arquivo XML e salva as informações.
* @access public
* @return this
*/
public function temp(){
if(!$this->info && !$this->current && !$this->next){
$apiUrl = $this->urlApi.'?weather='.urlencode($this->city).','.urlencode($this->state).','.urlencode($this->country).'&hl='.$this->language;
$xml = simplexml_load_string(
utf8_encode(
$this->getFile($apiUrl)
)
);
$info = $xml->xpath('/xml_api_reply/weather/forecast_information');
$current = $xml->xpath('/xml_api_reply/weather/current_conditions');
$next = $xml->xpath('/xml_api_reply/weather/forecast_conditions');
$this->info = $info[0];
$this->current = $current[0];
$this->next = $next;
}
return $this;
}
/**
* Retorna a data da consulta.
* @access public
* @return date
*/
public function getDate(){
return date( $this->dateformat, strtotime($this->info->forecast_date['data']) );
}
/**
* Retorna o nome da cidade e estado.
* @access public
* @return date
*/
public function getCityName(){
return $this->info->city['data'];
}
/**
* Retorna o icone do tempo atual.
* @access public
* @return string
*/
public function getCurrentTempIco(){
return $this->current->icon['data'];
}
/**
* Retorna a url completa do icone do tempo atual.
* @access public
* @return string
*/
public function getCurrentTempUrlIco(){
return rtrim($this->url,'/').'/'.$this->current->icon['data'];
}
/**
* Retorna a temperatura do dia atual.
* @access public
* @return string
*/
public function getCurrentTemp($temp='c'){
switch($temp)
{
case 'f':
return $this->current->temp_f['data'];
break;
case 'c':
default:
return $this->current->temp_c['data'];
break;
}
}
/**
* Retorna a condição do tempo do dia atual
* @access public
* @return string
*/
public function getCurrentCondition(){
return $this->current->condition['data'];
}
/**
* Carrega as informações do tempo dos próximos dias.
* @access public
* @return this
*/
public function getNextTemp(){
$this->day_of_week = $this->nextTempToArray();
return $this;
}
/*
private function nextTempToArray(){
//$next = array();
foreach ($this->next as $temp) {
$key = $this->caracter(utf8_decode($temp->day_of_week['data']));
$this->week[] = $key;
$next->{$key}->week = utf8_decode($temp->day_of_week['data']);
$next->{$key}->low = $temp->low['data'];
$next->{$key}->high = $temp->high['data'];
$next->{$key}->icon = rtrim($this->url,'/').'/'.$temp->icon['data'];
$next->{$key}->condition = $temp->condition['data'];
}
return $next;
}
*/
/**
* Cria o array com as informações do tempo dos próximos dias.
* @access private
* @return this
*/
private function nextTempToArray(){
//$next = array();
foreach ($this->next as $temp) {
$key = $this->caracter(utf8_decode($temp->day_of_week['data']));
$this->week[] = $key;
$next[$key]->week = utf8_decode($temp->day_of_week['data']);
$next[$key]->low = $temp->low['data'];
$next[$key]->high = $temp->high['data'];
$next[$key]->icon = rtrim($this->url,'/').'/'.$temp->icon['data'];
$next[$key]->condition = $temp->condition['data'];
}
return $next;
}
}
$tempo = new Weather();
?>
<h2>Previsão do Tempo - <?php echo $tempo->temp()->getDate(); ?></h2>
<p>Cidade: <?php echo $tempo->getCityName(); ?></p>
<h3>Previsão Atual</h3>
<table>
<tr>
<td><img src="<?php echo $tempo->getCurrentTempUrlIco(); ?>" alt="weather" /></td>
<td><?php echo $tempo->getCurrentTemp(); ?>&deg; C<br /><?php echo $tempo->getCurrentCondition(); ?></td>
</tr>
</table>
<h3>Próximos dias</h3>
<table>
<?php
foreach ($tempo->getNextTemp()->day_of_week as $temp) {
?>
<tr>
<td><?php echo $temp->week; ?></td>
<td><img src="<?php echo $temp->icon; ?>" alt="weather" /></td>
<td><?php echo $temp->low; ?>/<?php echo $temp->high; ?>&deg; C<br /><?php echo $temp->condition; ?></td>
</tr>
<?
}
?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment