接入教程
GET https://api.sunrise-sunset.org/json?lat=39.9&lng=116.41. 直接GET请求,无需密钥:
2. 返回sunrise、sunset、twilight等UTC时间。
3. 可加date参数指定日期。
4. 返回的是UTC时间,展示时需转换时区。
5. 使用需展示对 sunrise-sunset.org 的署名链接。
Python 获取日出日落
python
import requests
url = "https://api.sunrise-sunset.org/json"
r = requests.get(url, params={"lat":39.9,"lng":116.4})
data = r.json()["results"]
print(data["sunrise"], data["sunset"])
PHP 获取日出日落
php
<?php
$d = json_decode(file_get_contents("https://api.sunrise-sunset.org/json?lat=39.9&lng=116.4"), true);
echo $d["results"]["sunrise"] . " - " . $d["results"]["sunset"];
JavaScript 获取日出日落
javascript
const data = await fetch("https://api.sunrise-sunset.org/json?lat=39.9&lng=116.4").then(r => r.json());
console.log(data.results.sunrise, data.results.sunset);
常见问题
需要密钥吗?
不需要。免费使用、无需注册与API Key,仅需在应用或页面展示署名链接。
返回时间是本地时间吗?
返回的是UTC时间,需要在展示时转换为用户所在时区。
Aitishiku.com