首页 > 解决方案 > 如何使用 PHP 从 Openweathermap 中分离 JSON 响应数据?

问题描述

我试图从 PHP 的 OpenWeatherMap 获取温度和湿度值到我的网页。即使我能够与 api 链接进行通信并获得完整的数据。我无法单独分离并获取温度和湿度数据。

网站图片

我想要做的不是将 JSON 响应显示为一个块,而是将温度和湿度数据获取到网站的下方。

https://openweathermap.org/current -> 这里是包含 JSON 响应的链接,您可以查看标题“API 响应中的天气参数”下的响应。

这是代码:

<div class="column" style="background-color:#bbb;">
<?php
$url = "http://api.openweathermap.org/data/2.5/weather?q=Istanbul&mode=html&units=metric&APPID=...";     

$getweather = file_get_contents($url);

$data = json_decode($getweather);

$gettemperature = $data['main']['temp'];
$gethumidity =  $data['main']['humidity'];

echo $getweather;

?>

        <h3>Weather in Istanbul</h3>
        <span>Temperature: </span> <?php echo $gettemperature; ?><br>
        <span>Humidity: </span> <?php echo $gethumidity; ?><br>
</div>
<script>

不幸的是,我对 PHP 主题很陌生。我认为我的 PHP 语法有问题,或者尝试使用 PHP 做这一切都是错误的,所以我非常感谢您的支持。太感谢了。

标签: javascriptphphtmlopenweathermap

解决方案


推荐阅读