javascript - 如何使用 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 做这一切都是错误的,所以我非常感谢您的支持。太感谢了。
解决方案
推荐阅读
- java - 无法从 firebase 检索当前用户的数据
- mysql - Mysql 显示 #1222 - 使用的 SELECT 语句有不同的列数
- bash - 如何使用分隔符保存 Zenity 列表输出?
- cgroups - 是否可以将 cpu.shares 与 cpu.cfs_quota_us 一起使用
- dialogflow-es - 如何在 Dialogflow 上传的 CSV 文件中转义逗号?
- php - 如何在创建表时创建外键
- browser - 如何从开发者工具中查看浏览器本地存储中存储的 Json 对象
- c - 如何声明 volatile 结构的 C 数组
- c++ - 确定一个数组是否可以划分为两个子序列,每个子序列都具有递增的顺序
- python - Concatenate Pandas dataframe preferentially