How to get weather and weather information?

6

I came across a problem I'd like to see if it can be solved in a similar way to the mail . When you have a zip you can retrieve from a web service other data, such as the address and city of this zip informed.

In terms of climate, is there any way to get the weather information, temperature and current temperature of that place (the intention was then to stylise the css of this part of the page to make it look nice) ?

    
asked by anonymous 08.03.2016 / 20:56

6 answers

7

You can get this information through weather service APIs such as Yahoo!

I've created and tested an example usage, as shown below:

<script>
    var callbackFunction = function(data) {
        console.log(data);
    };
</script>

<script src="https://query.yahooapis.com/v1/public/yql?q=select*fromweather.forecastwherewoeidin(selectwoeidfromgeo.places(1)wheretext='sãopaulo,sp')&format=json&callback=callbackFunction"></script>

Where:

  • format : it is the type of data that will be returned, being JSON or XML;
  • callback : this is the function that will handle the response of your request.

You can also add

and u = 'c'

in the WHERE clause to change to the metric system and scale to Celsius.

If it's just a city, you can search the WOEID before to improve the response time.

For example, for São Paulo,

link

I would return 455827, where the final query would be:

select * from weather.forecast where woeid = 455827&format=json&callback=callbackFunction

There is this site , own Yahoo !, where you can test your YQL (Yahoo Query Language).

It is worth mentioning that according to the Yahoo API, the limit of daily queries is 2 thousand requisitions, and pay attention to the terms of use, where it can only be used by individuals, non-profit companies and non-commercial use.

Remember that the Yahoo! API is only one of the solutions, if it does not meet your needs, you can search for 'weather api' or 'weather api' forecast.

    
08.03.2016 / 21:13
5

I found this 'API' which returns the following data:

{
 "cidade":"Sao Paulo - SP",
 "agora":{
  "data_hora":"08\/11\/2013 - 19:59",
  "descricao":"Muito Nublado (noite)",
  "temperatura":"19",
  "humidade":"73%",
  "visibilidade":"9,99 km",
  "vento_velocidade":"20,92 km\/h",
  "vento_direcao":"SE",
  "pressao":"1.015,92 mBar",
  "pressao_status":"subindo",
  "nascer_do_sol":"6:16 am",
  "por_do_sol":"7:25 pm",
  "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/27.png"
 },
 "previsoes":[
  {
   "data":"Sexta - 08\/11\/2013",
   "descricao":"Parcialmente Nublado (noite)",
   "temperatura_max":"23",
   "temperatura_min":"16",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/29.png"
  },
  {
   "data":"S\u00e1bado - 09\/11\/2013",
   "descricao":"Tempo Bom (dia)",
   "temperatura_max":"27",
   "temperatura_min":"18",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/34.png"
  },
  {
   "data":"Domingo - 10\/11\/2013",
   "descricao":"Ensolarado",
   "temperatura_max":"29",
   "temperatura_min":"21",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/32.png"
  },
  {
   "data":"Segunda - 11\/11\/2013",
   "descricao":"Parcialmente Nublado (dia)",
   "temperatura_max":"32",
   "temperatura_min":"23",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/30.png"
  },
  {
   "data":"Ter\u00e7a - 12\/11\/2013",
   "descricao":"Parcialmente Nublado (dia)",
   "temperatura_max":"30",
   "temperatura_min":"19",
   "imagem":"http:\/\/developers.agenciaideias.com.br\/images\/tempo\/30.png"
  }
 ]
}

Follow the url: link

I tested the request described in the documentation, with Angularjs, and returned everything correctly. =)

    
08.03.2016 / 21:19
4

Climatempo (Brazilian meteorological company) launched an API in 2017, you can preach the forecast for free.

The site is link

There you have all the documentation. Just log in with your "Facebook", "Google" or "Github", generate a token and make the request via GET.

    
16.01.2018 / 13:39
2

If it is via programming with C # I recommend using the Html Agility Pack, with it you can tell the source of the information (Site) and inform the code in which div is the information you want to be able to use for various purposes, I used it to get the mega-results.

link

    
09.03.2016 / 04:35
1

A JSON alternative is: link

Just change the city name followed by the state. Take accents, example:

linhares_es,
saopaulo_sp,
vitoria_es,
riodejaneiro_rj
    
12.07.2016 / 17:28
1

Based on the answer from silvalexandre
Use the link above to find out the location woeid

    select woeid from geo.places(1) where text='são paulo, sp'

Javascript Call lib jquery and

    <script>
    $.get('https://query.yahooapis.com/v1/public/yql',{
        q:'select * from weather.forecast where woeid in (455822)',
        format:'json'
    },function (res) {
        if(res){
            umi = res.query.results.channel.atmosphere.humidity;
            temp = res.query.results.channel.item.condition.temp;

            temp = Math.round((temp-32)/1.8); //F to C

            console.log(temp,umi);
        }
    },'json');
    </script>

PHP

$cont = file_get_contents("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(455822)&format=json");

$cont = json_decode($cont,1);

$umi = $cont['query']['results']['channel']['atmosphere']['humidity'];
$temp = $cont['query']['results']['channel']['item']['condition']['temp'];

$temp = intval( ($temp - 32) /1.8 ); //F to C

echo $umi." ".$temp;
    
20.06.2017 / 16:54