【问题标题】:Get Weather Forecast Data获取天气预报数据
【发布时间】:2012-11-01 15:24:00
【问题描述】:

我想开发一个天气网络服务,我只想获取天气预报的信息。

比我创建自己的界面。但我找不到如何访问这些数据。

如何访问天气数据?谢谢。

【问题讨论】:

    标签: weather-api google-weather-api yahoo-weather-api


    【解决方案1】:

    我在 mashape 上创建了一个Weather API,他们有一个可以使用的简单 PHP SDK。 这个 api 使用起来非常简单,因为我们使用了当今可用的很酷的标准,例如 JSON 和 REST。

    如果你喜欢它,请在mashape尝试一下

    【讨论】:

      【解决方案2】:

      您可能正在寻找直接来自源的数据。我对这个other stackoverflow question 提供了一个非常全面的概述,但以下是基础知识:

      数据来源

      获取数据的最佳地点是国家气象局及其附属机构。他们不仅对美国,而且对整个世界都有预测。要获得所需的数据量,您需要务实地访问它……这些可能是您最好的选择:

      http://nomads.ncdc.noaa.gov/dods/ http://nomads.ncep.noaa.gov/dods/

      将数据记录到数据库中

      根据您需要的数据量,您可能希望将所有内容存储在数据库中...这样可以快速访问和提供服务。一个不错的选择是使用 MySQL 或 PostGRES 以及 PyDAP 或 NetCDF。你可以选择这个 python 预测模块,它结合了 PyDAP 和 PostGRES:

      http://getforecasting.com

      主页上下载一天内整个预测的示例:

      from forecasting import Model
      
      rap = Model('rap')
      rap.connect(database='weather', user='chef')
      fields = ['tmp2m']
      rap.transfer(fields)
      

      使用数据

      有了数据库中的数据,查询您需要的任何内容一样简单。例如,从快速刷新模型中获取最新预测:

      select
      ST_X(geom),
      ST_Y(geom),
      value
      from data
      inner join gridpoints gp on data.gridid = gp.gridpointid
      inner join forecasts fcst on data.forecastid = fcst.forecastid
      inner join fields fld on fcst.fieldid = fld.fieldid
      inner join models m on fld.modelid = m.modelid
      where 
      m.name = 'rap'
      and fld.name = 'tmp2m'
      and fcst.datatime = (select max(datatime) from forecasts where forecasts.fieldid = fld.fieldid)
      and fcst.datatimeforecast = (select min(datatimeforecast) from forecasts where forecasts.datatime = fcst.datatime)
      order by gp.ord;
      

      【讨论】:

        【解决方案3】:

        您可以使用 yahoo API。以下是示例(在 PHP 中):

            if(isset($_POST['zipcode']) && is_numeric($_POST['zipcode'])){
                $zipcode = $_POST['zipcode'];
            }else{
                $zipcode = '50644';
            }
            $result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=f');
            $xml = simplexml_load_string($result);
        
            //echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
        
            $xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0');
            $location = $xml->channel->xpath('yweather:location');
        
            if(!empty($location)){
                foreach($xml->channel->item as $item){
                    $current = $item->xpath('yweather:condition');
                    $forecast = $item->xpath('yweather:forecast');
                    $current = $current[0];
                    $output = <<<END
                        <h1 style="margin-bottom: 0">Weather for {$location[0]['city']}, {$location[0]['region']}</h1>
                        <small>{$current['date']}</small>
                        <h2>Current Conditions</h2>
                        <p>
                        <span style="font-size:72px; font-weight:bold;">{$current['temp']}&deg;F</span>
                        <br/>
                        <img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>&nbsp;
                        {$current['text']}
                        </p>
                        <h2>Forecast</h2>
                        {$forecast[0]['day']} - {$forecast[0]['text']}. High: {$forecast[0]['high']} Low: {$forecast[0]['low']}
                        <br/>
                        {$forecast[1]['day']} - {$forecast[1]['text']}. High: {$forecast[1]['high']} Low: {$forecast[1]['low']}
                        </p>
            END;
                }
            }else{
                $output = '<h1>No results found, please try a different zip code.</h1>';
            }
            ?>
            <html>
            <head>
            <title>Weather</title>
            <style>
            body {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
            }
            label {
                font-weight: bold;
            }
            </style>
            </head>
            <body>
            <form method="POST" action="">
            <label>Zip Code:</label> <input type="text" name="zipcode" size="8" value="" /><br /><input type="submit" name="submit" value="Lookup Weather" />
            </form>
            <hr />
            <?php echo $output; ?>
        

        【讨论】:

          猜你喜欢
          • 2019-06-10
          • 2011-05-16
          • 2013-06-22
          • 2016-06-10
          • 1970-01-01
          • 2015-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多