php - 在 PHP 中使用 OpenWeatherMap 预报 API
问题描述
我正在尝试从 openweathermap 显示一个城市的预测。但我的 foreach 什么也没显示。怎么了?
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?zip=85080,de&lang=de&APPID=MYKEY";
$contents = file_get_contents($url);
$clima = json_decode($contents, true);
foreach($clima as $data) {
echo $data->list->main->temp_min;
}
?>
解决方案
a 的结果json_decode(string, true)
是一个关联数组。
<?php
$url = "http://api.openweathermap.org/data/2.5/forecast?zip=85080,de&lang=de&APPID=MYKEY";
$contents = file_get_contents($url);
$clima = json_decode($contents, true);
foreach($clima['list'] as $data) {
echo $data['main']['temp_min'];
}
?>
如果要使用对象语法,请不要将关联设置为true
.
$clima = json_decode($contents);
foreach($clima->list as $data) {
echo $data->main->temp_min;
}
推荐阅读
- macos - 如何在 NSScrollView 中显示非常大的基于 Metal 的纹理?
- python - Twint 搜索不适用于日期参数(python)
- sql-server - 在始终加密的 mssql 数据库中插入数据时,在 mulesoft 中获取加密架构不匹配
- forms - 没有获取使用 go 发送的表单数据
- graphql - 在 Apollo-client 中,writeQuery 会更新缓存,但 UI 组件不会重新渲染
- java - 显示和隐藏布局
- c++ - 使用 switch case 而不是 if else
- r - 不存在组合时的R重塑功能
- python - 如何使用 django-filter 为每个属性选择创建一个复选框/单选按钮
- javascript - 如何使用tailwind css在同一个html页面上选择多个按钮选项?