雅虎天气 API 对某些美国纬度/经度位置给出错误
我正在创建有关公园的页面。对于每个公园,都有一个纬度/经度,对于大多数公园,我可以使用雅虎天气 API
但由于某种原因,某些页面返回如下错误:
Invalid Input /forecastrss?w=2347563
After a call that I make雅虎天气 API 如下所示:
http://www.geomojo.org/cgi-bin /reversegeocoder.cgi?long=-121.433098&lat=40.509312
任何想法我怎样才能解决这些错误?
这是我的代码:
$url = 'http://www.geomojo.org/cgi-bin/reversegeocoder.cgi?long='.$lng.'&lat='.$lat;
// Calls the url to get the zip code and woeid
$webpage = file_get_contents($url);
//echo $webpage;
try
{
$xml = new SimpleXMLElement($url, 0, true);
// Gets the woeid to look up the weather in that specific place dynamically.
$woeid= $xml->woeid; // Displays "Text"
$zip = $xml->name; // Displays "Text"
// URL to send to yahoo weather to get weather RSS
$yahoo_url = 'http://weather.yahooapis.com/forecastrss?w='.$woeid;
$xml = file_get_contents($yahoo_url);
$yahoo_response = new SimpleXMLElement($xml , 0, false);
$weather_description = $yahoo_response->channel->item->description;
$splitdata = explode('<a', $weather_description);
echo '<p>'.$splitdata[0].'</p>';
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
这是一个不起作用的示例的网址。请参阅左侧的天气部分:
I am creating pages about parks. For each park, there is a lat/lng and for most parks, I am able to use the Yahoo weather API
But for some reason, some pages return errors like this:
Invalid Input /forecastrss?w=2347563
After a call that I make like this to the Yahoo weather API:
http://www.geomojo.org/cgi-bin/reversegeocoder.cgi?long=-121.433098&lat=40.509312
Any idea how I can resolve these sorts of errors?
Here is my code:
$url = 'http://www.geomojo.org/cgi-bin/reversegeocoder.cgi?long='.$lng.'&lat='.$lat;
// Calls the url to get the zip code and woeid
$webpage = file_get_contents($url);
//echo $webpage;
try
{
$xml = new SimpleXMLElement($url, 0, true);
// Gets the woeid to look up the weather in that specific place dynamically.
$woeid= $xml->woeid; // Displays "Text"
$zip = $xml->name; // Displays "Text"
// URL to send to yahoo weather to get weather RSS
$yahoo_url = 'http://weather.yahooapis.com/forecastrss?w='.$woeid;
$xml = file_get_contents($yahoo_url);
$yahoo_response = new SimpleXMLElement($xml , 0, false);
$weather_description = $yahoo_response->channel->item->description;
$splitdata = explode('<a', $weather_description);
echo '<p>'.$splitdata[0].'</p>';
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
Here is a url of an example where this doesn't work. See the weather section on the left side:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您通过雅虎同时进行纬度/经度->woeid 和天气呼叫,您可能会得到更好的结果。您可以使用 YQL 为您执行这两个调用。
例如,使用上面的纬度/经度对,这里有一个 YQL 查询,它首先将纬度/经度对转换为 woeid,然后使用它来获取预测:
select * from Weather.woeid where w in (select woeid from geo.placefinder where text="-121.433098, 40.509312" and gflags="R")
何时在YQL控制台中尝试,结果如下:
You might get better results if you do both the lat/long->woeid and weather calls through Yahoo. You can use YQL to do both calls for you.
For example, using your lat/long pair above, here's a YQL query that first converts the lat/long pair into a woeid, then uses that to get the forecast:
select * from weather.woeid where w in (select woeid from geo.placefinder where text="-121.433098, 40.509312" and gflags="R")
When tried in the YQL console, here are the results:
此地理编码调用
http://www.geomojo.org/cgi-bin/reversegeocoder.cgi?long=-121.433098&lat=40.509312
仅返回加利福尼亚州。
如果我必须做出大胆的猜测,我会说天气 API 需要一个比这更具体的位置。这可能是因为有些公园太偏远(或者最近的城镇太小),以至于地理编码器无法找到您所经过的坐标所在的城市。
This geocoding call
http://www.geomojo.org/cgi-bin/reversegeocoder.cgi?long=-121.433098&lat=40.509312
is only returning a state, California.
If I had to make a wild guess, I'd say the weather API needs a more specific location than that. This may be because some parks are so remote (or the nearest town so small) that the geocoder can't find a city for the coordinates you're passing.