一个免费的天气接口
一个免费的天气接口[*]接口地址:http://wthrcdn.etouch.cn/weather_mini
[*]
[*]请求方式:get
[*]
[*]参数:city (必填)
[*]
示例:http://wthrcdn.etouch.cn/weather_mini?city=郑州
获取:
{
"data": {
"yesterday": {
"date": "27日星期二",
"high": "高温 33℃",
"fx": "东北风",
"low": "低温 25℃",
"fl": "<!]>",
"type": "多云"
},
"city": "郑州",
"forecast": [
{
"date": "28日星期三",
"high": "高温 30℃",
"fengli": "<!]>",
"low": "低温 25℃",
"fengxiang": "东北风",
"type": "中雨"
},
{
"date": "29日星期四",
"high": "高温 32℃",
"fengli": "<!]>",
"low": "低温 23℃",
"fengxiang": "东北风",
"type": "多云"
},
{
"date": "30日星期五",
"high": "高温 35℃",
"fengli": "<!]>",
"low": "低温 25℃",
"fengxiang": "南风",
"type": "晴"
},
{
"date": "31日星期六",
"high": "高温 35℃",
"fengli": "<!]>",
"low": "低温 27℃",
"fengxiang": "南风",
"type": "小雨"
},
{
"date": "1日星期天",
"high": "高温 34℃",
"fengli": "<!]>",
"low": "低温 23℃",
"fengxiang": "东风",
"type": "小雨"
}
],
"ganmao": "感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。",
"wendu": "26"
},
"status": 1000,
"desc": "OK"
}
[*]
[*]获取到的数据为JSON字符串 如需使用用JSON.parse()转为js对象
[*]
使用原生js调取接口:
<body>
<input type="text" id="aaa" />
<button id="btn">查询</button>
<script>
btn.onclick = function () {
let a = document.querySelector("#aaa");
console.log(a);
const xhr = new XMLHttpRequest();
xhr.open(
"get",
`http://wthrcdn.etouch.cn/weather_mini?city=${a.value}`
);
xhr.send();
xhr.onload = function () {
xhr.status;
if (xhr.status == 200) {
xhr.responseText;
let b = JSON.parse(xhr.responseText);
console.log(b);
let arr = b.data;
console.log(arr);
}
};
};
</script>
</body>
在控制台查看获取到的数据!! 为什么我这样写获取不到任何信息呢
<?php
header("Content-type: text/html; charset=utf-8");
$json_url = 'http://wthrcdn.etouch.cn/weather_mini?city=%E6%97%A0%E9%94%A1';
$content = file_get_contents($json_url);
$result = json_decode($content, true);
?>
<html>
<head>
<title>天气</title>
</head>
<body>
<p>当前城市:<?php echo $result['data']['city']; ?></p>
<p>天气状况:<?php echo $result['data']['forecast']['type']; ?></p>
<p>最高温度:<?php echo $result['data']['forecast']['high']; ?></p>
<p>最低温度:<?php echo $result['data']['forecast']['low']; ?></p>
<table class="table table-striped table-bordered" style="margin-left: 20px;width: 200px">
<thead>
<th>实时天气信息</th>
</thead>
<tbody>
<?php
echo "<tr><td>当前城市:</td><td>".$result->data->city."</td></tr>";
echo "<tr><td>今日天气:</td><td>".$result['data']['forecast']['type']."</td></tr>";
echo "<tr><td>今日气温:</td><td>".$result['data']['wendu']."℃</td></tr>";
echo "<tr><td>今日风向:</td><td>".$result['data']['forecast']['fengxiang']."</td></tr>";
echo "<tr><td>今日风力:</td><td>".$result['data']['forecast']['fengli']."</td></tr>";
echo "<tr><td>更新时间:</td><td>".$result['data']['forecast']['date']."</td></tr>";
?>
</tbody>
</table>
</body>
</html> 闷骚小贱男 发表于 2021-7-28 13:14
....看到那么多要国外天气的..
百度天气不香吗?
https://www.baidu.com/s?wd=%E4%BC%91%E6%96%AF%E9%A1%B ...
接口有吗 有没有能获取国外天气的接口? 感谢楼主分享 不错,就是不知道是否稳定 感谢分享 奥利给,感谢分享 感谢分享 感谢分享,可以做个捷径 比较好的了 不错 感谢分享