PHP 获取 OpenWeatherMap 数据

Jan*_*pan 1 php api weather-api openweathermap

我想从OWM API获取天气数据,在本例中我想获取温度描述信息。我如何通过 PHP 从他们的 API 中“拉”这个?

3y3*_*l3r 5

其实很简单,看一下这段代码。

\n\n
<?php\n\n //get JSON\n $json = file_get_contents('http://api.openweathermap.org/data/2.5/find?q=Calabar,NG&type=accurate&mode=jso\xe2\x80\x8c\xe2\x80\x8bn');\n\n //decode JSON to array\n $data = json_decode($json,true);\n\n //show data\n var_dump($data);\n\n //description\n echo $data['weather'][0]['description'];\n //temperature\n echo $data['main']['temp'];\n\n\n?> \n
Run Code Online (Sandbox Code Playgroud)\n\n

首先,您需要使用 获取文件/字符串function file_get_contents(),在本例中它是 JSON 字符串。之后您需要使用 function 解码该字符串json_decode()。参数true意味着我们想要将此字符串解析为数组而不是对象。执行此操作后,您可以使用此数据集,因为它是简单变量类型的数组。就这样。

\n\n

编辑:

\n\n

根据下面的Prodigy评论编辑了 URL

\n