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