Skip to content

Instantly share code, notes, and snippets.

@hewerthomn
Created April 21, 2013 04:54
Show Gist options
  • Save hewerthomn/5428523 to your computer and use it in GitHub Desktop.
Save hewerthomn/5428523 to your computer and use it in GitHub Desktop.
Página php que acessa API da wunderground e escreve a cidade, pais, umidade, temperatura e hora atual. Exemplo: <Porto Velho/RO|89% 23C|10:39>
<?php
/**
* API Index
*/
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
$service = $_GET['s'];
date_default_timezone_set('America/Porto_Velho');
/**
* Usa a API da wunderground para chckar a
* @return array
*/
function get_temperature() {
/**
* create a account and get your API key at
* http://www.wunderground.com/
*/
$APIKEY = "";
//$json_string = file_get_contents("http://api.wunderground.com/api/{$APIKEY}/conditions/q/BR/Porto_Velho.json");
$json_string = file_get_contents("http://api.wunderground.com/api/{$APIKEY}/conditions/q/SBPV.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'current_observation'}->{'display_location'}->{'city'};
$location .= '/' . $parsed_json->{'current_observation'}->{'display_location'}->{'country_iso3166'};
$temp_c = $parsed_json->{'current_observation'}->{'temp_c'};
$relative_humidity = $parsed_json->{'current_observation'}->{'relative_humidity'};
/**
* preenche com espaços se necessário até o tamanho indicado
*/
$location = str_pad($location, 16);
$temperature = str_pad("{$relative_humidity} {$temp_c}C", 11);
return array(
'city' => $location,
'temp' => $temperature,
'time' => date('h:i')
);
}
$data = get_temperature();
$result = "<{$data['city']}|{$data['temp']}|{$data['time']}>";
die($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment