5

我有 openweathermap api 密钥,但如何在 PHP 中使用它?并且天气报告应该来自城市名称,而不是来自位置天气 ID

4

1 回答 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

于 2014-08-29T20:27:46.460 回答