Skip to content

Instantly share code, notes, and snippets.

@maiconschmitz
Created March 24, 2012 18:37
Show Gist options
  • Save maiconschmitz/2186269 to your computer and use it in GitHub Desktop.
Save maiconschmitz/2186269 to your computer and use it in GitHub Desktop.
Wrapper para consulta à API do Google Weather
function exibirPrevisaoTempo(icone, cidade, condicao, temperatura, umidade, vento)
{
var conteudoDialog;
conteudoDialog =
'<p><h2>' + cidade + '</h2></p>' +
'<p><img src="http://img0.gmodules.com/' + icone + '"> <b>' + condicao +'</b></p>' +
'<p>Temperatura: ' + temperatura + ' °C</p>' +
'<p>Vento: ' + vento + '</p>' +
'<p>' + umidade + '</p>';
$('#resultado-previsao').html(conteudoDialog);
$('#resultado-previsao').dialog({
autoOpen: false,
width: 300,
buttons: {
"Ok": function() {
$(this).dialog("close");
}
}
});
$('#resultado-previsao').dialog('open');
}
function obterPrevisaoTempo(campos)
{
$.ajax({
type: "GET",
url: "previsao-tempo.php",
data: campos,
success: function(xml) {
processaXML(xml);
},
error: function(erro) {
alert('Ocorreu um erro ao obter a previsão do tempo.');
}
});
}
function processaXML(xml)
{
var icone;
var cidade;
var condicao;
var temperatura;
var umidade;
var vento;
$(xml).find('forecast_information').each(function(){
cidade = $(this).find('city').attr('data');
});
$(xml).find('current_conditions').each(function(){
condicao = $(this).find('condition').attr('data');
temperatura = $(this).find('temp_c').attr('data');
umidade = $(this).find('humidity').attr('data');
vento = $(this).find('wind_condition').attr('data');
icone = $(this).find('icon').attr('data');
});
exibirPrevisaoTempo(icone, cidade, condicao, temperatura, umidade, vento);
}
<?php
/**
* Retorna dados referentes à temperatura da cidade
* @author Maicon Gabriel Schmitz
* @param string $cidade Cidade
* @param string $estado Estado
* @param string $pais País
* @param string $idioma Idioma
* @return string XML
*/
function temperaturaCidade($cidade,$estado,$pais,$idioma)
{
$retorno = "";
$bufferLeitura = "";
/* monta a url de pesquisa ao nome da cidade */
$urlCidade = "http://www.google.com/ig/api?weather={$cidade},{$estado},{$pais}&hl={$idioma}";
$url = parse_url($urlCidade);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$fp = fsockopen($host, 80, $errno, $errstr, 1) or
die('Não foi possível abrir conexão com o servidor do google.');
if ($fp)
{
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
while (!feof($fp)) {
$bufferLeitura .= fgets($fp, 128);
}
$linhas = split("\n", $bufferLeitura);
$retorno = $linhas[count($linhas)-1];
fclose($fp);
}
/* retorna o XML */
return utf8_decode($retorno);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment