我想开发一个天气web服务,我只想获得天气预报的信息。
然后我创建我自己的界面。但我找不到访问这些数据的方法。
如何获取天气数据?谢谢。
发布于 2013-01-29 20:06:03
我在mashape上提供了一个Weather API,他们有一个简单的PHP SDK可以使用。这个api使用起来非常简单,因为我们使用了当今可用的很酷的标准,比如JSON和REST。
如果你喜欢,请在mashape上试一试
发布于 2014-03-01 05:01:28
您可能正在寻找的是直接来自源的数据。我对这个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;发布于 2013-01-22 02:01:38
您可以使用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']}°F</span>
<br/>
<img src="http://l.yimg.com/a/i/us/we/52/{$current['code']}.gif" style="vertical-align: middle;"/>
{$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; ?>https://stackoverflow.com/questions/13356673
复制相似问题