我正在使用 openweathermap.org api,它为我提供了不正确的输出。
如果我点击此网址,我会得到以下输出:
"{"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 curl
或 file_get_contents
调用相同的 url,我会得到以下输出:
"{"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}"
它们为什么不同?
我没有看到问题。它返回完全相同的经度(-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 列表(lat
&lon
参数,以您的纬度和经度作为值:
http://api.openweathermap.org/data/2.5/weather?appid=35d3153a253e2536f49f02fd8080dfc2&lat=37.83&lon=-121.96
(您还可以在此支持的城市列表中找到您所在城市的纬度和经度(
有关更多详细信息,请参阅 API 文档。