3

我正在使用openweathermap.org api,它提供了不正确的输出。

如果我点击这个 url,我会得到以下输出:

"{"coord":{"lon":-121.96,"lat":37.83},"weather":[{"id":721,"main":"Haze","description":"haze","icon":"50n"}],"base":"stations","main":{"temp":281.21,"pressure":1030,"humidity":81,"temp_min":273.15,"temp_max":285.15},"visibility":11265,"wind":{"speed":1.07,"deg":54.0019},"clouds":{"all":1},"dt":1454739836,"sys":{"type":1,"id":409,"message":0.0189,"country":"US","sunrise":1454771247,"sunset":1454809012},"id":5342970,"name":"Diablo","cod":200}"

如果我通过 phpcurl或调用相同的 url file_get_contents,我会得到以下输出:

"{"coord":{"lon":-121.96,"lat":37.83},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"base":"cmc stations","main":{"temp":275.178,"pressure":1022.49,"humidity":83,"temp_min":275.178,"temp_max":275.178,"sea_level":1043.42,"grnd_level":1022.49},"wind":{"speed":1.07,"deg":356.501},"clouds":{"all":12},"dt":1454738179,"sys":{"message":0.0112,"country":"US","sunrise":1454771247,"sunset":1454809011},"id":5342970,"name":"Diablo","cod":200}"

为什么它们不同?

4

1 回答 1

0

我没有看到问题。它为经度 ( )、纬度 ( )、城市 ID ( ) 和城市名称 ( )返回完全相同的值,这清楚地表明两个结果代表来自同一位置的结果。-121.9637.835342970Diablo

两个结果之间的微小差异可能是由于天气的微小变化或从不同的服务器或气象站获取的结果(base属性的不同值似乎表明)造成的。

我不会为这些微小的差异而烦恼。但是,我确实有另一个担忧(请参阅下面的注释)。


笔记 :

参数的行为zip不可靠。当我从我的位置(比利时)在浏览器中打开您的 URL 时,有时会得到预期的结果,有时会出现以下错误:

{"cod":"404","message":"Error: Not found city"}

为避免此问题,最好使用以下选项之一:

  • q参数,城市名称和国家作为值:

    http://api.openweathermap.org/data/2.5/weather?appid=35d3153a253e2536f49f02fd8080dfc2&q=Diablo,US

  • id参数,以您的城市 ID 作为值:

    http://api.openweathermap.org/data/2.5/weather?appid=35d3153a253e2536f49f02fd8080dfc2&id=5342970
    (您可以在此处 下载所有支持的城市 ID 列表)

  • lat&lon参数,以您的纬度和经度为值:

    http://api.openweathermap.org/data/2.5/weather?appid=35d3153a253e2536f49f02fd8080dfc2&lat=37.83&lon=-121.96
    (您也可以在此支持的城市列表中 找到您所在城市的经纬度)

有关更多详细信息,请参阅API 文档

于 2016-03-11T08:15:21.287 回答