php - PHP:带有标准类对象的数组
问题描述
我正在开发一个用于天气预报的应用程序和网站,我正在使用一家名为 aerisweather 的公司,它有一个 api 并使用 json 数据进行响应。在这一点上,我每天都在学习编码,但是我在处理这个 api 响应时遇到了问题……我正在使用 PHP 尝试通过数组来提取数据。
这是他们页面上的典型 api 请求
<?php
// fetch Aeris API output as a string and decode into an object
$response = file_get_contents("https://api.aerisapi.com/observations/closest?p=:auto&format=json&radius=50mi&filter=allstations&limit=1&fields=id,ob.dateTimeISO,ob.tempF,ob.dewpointF,ob.humidity,ob.windSpeedMPH,ob.windDir,ob.weather,ob.heatindexF,ob.feelslikeF&client_id=CLIENT_ID&client_secret=CLIENT_SECRET");
$json = json_decode($response);
if ($json->success == true) {
// create reference to our returned observation object
print_r($json);
}
else {
echo sprintf("An error occurred: %s", $json->error->description);
}
?>
响应是
{
"success": true,
"error": null,
"response": [
{
"id": "MID_C3868",
"ob": {
"dateTimeISO": "2020-07-02T23:48:03-04:00",
"tempF": 73,
"dewpointF": 69,
"humidity": 86,
"windSpeedMPH": 0,
"windDir": "W",
"weather": "Clear",
"heatindexF": 73,
"feelslikeF": 73
}
}
]
}
我基本上需要了解如何访问这些值,以便我可以将它们放入 html 页面并制作一个很好的观察页面。
我尝试通过在 json_decode (响应)之后添加“,true”然后检查 var_dump 将所有内容更改为数组,这就是我得到的
C:\wamp64\www\aeris\aerisobs.php:10:
array (size=3)
'success' => boolean true
'error' => null
'response' =>
array (size=5)
0 =>
array (size=2)
'id' => string 'MID_C3868' (length=9)
'ob' =>
array (size=8)
...
1 =>
array (size=2)
'id' => string 'MID_D7873' (length=9)
'ob' =>
array (size=8)
...
2 =>
array (size=2)
'id' => string 'MID_NHSLM' (length=9)
'ob' =>
array (size=8)
...
3 =>
array (size=2)
'id' => string 'PWS_KMAMETHU12' (length=14)
'ob' =>
array (size=8)
...
4 =>
array (size=2)
'id' => string 'MID_F5795' (length=9)
'ob' =>
array (size=8)
...
请告诉我如何做到这一点......我将永远感激不尽。
我的目标是在我的网站上有一个看起来像这样的部分
Current Temperature: 'tempF' value from the api
Dew Point: 'dewpointF' value from the api
等等...
感谢您提供的任何和所有帮助!
谦虚,
贾斯汀
解决方案
推荐阅读
- html - Bootstrap 列在不应该时垂直堆叠
- html - 为什么 gatsby-plugin-image 不显示,但如果我使用 gatsby-background-image 它会显示?我正在使用 GraphiQL
- ruby - Ruby - 全局更改时区
- apache-spark - SQLServer大数据集群和Yarn及应用列表
- julia - Julia UndefVarError 关于元编程
- amazon-quicksight - 如何使用开始和结束日期字段创建 QuickSight 直方图可视化?(甘特图)
- php - 数组中的表单和对象
- linux - 如何在 clang++ 7.0.1 中识别目标文件的来源?
- python-3.x - Python - 用替换打破长 F-String
- r - 如何在 R's Fable 包中模拟加权 ARIMA 模型