OpenWeatherMap API生成错误的输出

时间:2016-02-06 07:06:52

标签: php json curl weather-api openweathermap

我正在使用openweathermap.org api,它为我提供了错误的输出。

如果我点击this 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}"

如果我通过php curlfile_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}"

为什么他们不同?

1 个答案:

答案 0 :(得分:0)

我没有看到问题所在。它会为经度(-121.96),纬度(37.83),城市ID(5342970)和城市名称(Diablo返回完全相同的值),这清楚地表明两个结果都代表来自同一地点的结果。

两个结果之间的微小差异可能是由于天气的微小变化或从不同服务器或气象站获取的结果(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 here

  • 的列表
  • lat& lon个参数,纬度和经度为值:

    http://api.openweathermap.org/data/2.5/weather?appid=35d3153a253e2536f49f02fd8080dfc2&lat=37.83&lon=-121.96
    (您还可以在this list of supported cities

  • 中找到您所在城市的纬度和经度

有关详细信息,请参阅the API docs