如何使用openweathermap的API密钥?

5
我有openweathermap的API密钥,但我如何在PHP中使用它? 而且天气报告应该根据城市名称进行报告,而不是根据位置天气ID进行报告。

我得到了以下内容: {"coord":{"lon":-0.13,"lat":51.51},"sys":{"type":1,"id":5091,"message":0.3048,"country":"GB","sunrise":1409288950,"sunset":1409338405},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":290.39,"pressure":1013,"humidity":77,"temp_min":289.15,"temp_max":291.65},"wind":{"speed":7.2,"deg":240},"clouds":{"all":75},"dt":1409343901,"id":2643743,"name":"London","cod":200}那么,我该如何将“[temp] => 300.15”中的值存储到一个变量中,并显示出来? - Nayanendu Mondal
2
这个问题应该重新开放。"太宽泛",胡说八道 :D 这是原帖作者正在寻找的完美答案。关闭这个问题的人严重过于狂热 :) - Agent Zebra
1个回答

13

如何使用API密钥

在GET请求中添加以下参数:APPID = APIKEY 例如:api.openweathermap.org/data/2.5/forecast/city?APPID=YOURAPIKEY & 其他你想请求的内容。

<?php

    $request = 'http://api.openweathermap.org/data/2.5/forecast/city?APPID=***YOURAPIKEY***';
    $response  = file_get_contents($request);
    $jsonobj  = json_decode($response);
    print_r($jsonobj);
?>

若要请求特定信息,只需查看API接受的键并将&附加到URL KEY=VAL的末尾。

例如:

http://api.openweathermap.org/data/2.5/weather?APPID=YourAPIKey&q=London

此外,在使用API时,我建议安装JSON查看器插件。我安装了JSONView作为Google Chrome扩展程序,非常适合查看JSON。

https://chrome.google.com/webstore/search/jsonview?hl=en


2
能否在头部添加 API 密钥? - bluelurker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接