Logo
Section 1 / 2

使用APOD API展示精选图片

学习如何使用APOD API获取每日天文图片。

导入

使用APOD API展示精选图片

学习如何使用APOD API获取每日天文图片。

自1995年以来,NASA在其网站上每日发布一张宇宙相关的图片,并附有简短说明。这张精选图片被称为每日天文图(Astronomy Picture of the Day, APOD)。这些图片被存档在NASA的数据库中,可以通过APOD API进行访问。在本课程中,我们将学习如何使用该API获取每日天文图。

...
获取APOD的接口地址

查询参数类型分类说明
date字符串可选获取指定日期的APOD,默认为当前日期
start_date字符串可选指定获取APOD的时间范围起始日期,不能与date同时使用
end_date字符串可选指定获取APOD的时间范围结束日期,默认为当前日期,必须配合start_date使用
count整数可选设置返回的APOD数量,不能与date、start_date或end_date同时使用
thumbs布尔值可选决定是否返回视频缩略图的URL,若APOD不是视频则此参数无影响,可选值为true或false
api_key字符串必需用于验证请求的API密钥
1const API_KEY = '{{API_KEY}}'; 2 3const endpointUrl = new URL('https://api.nasa.gov/planetary/apod'); 4 5const queryParameters = new URLSearchParams({ 6 api_key: API_KEY 7}); 8 9const options = { 10 method: 'GET' 11}; 12 13async function fetchAPOD() { 14 try { 15 endpointUrl.search = queryParameters; 16 const response = await fetch(endpointUrl, options); 17 18 printResponse(response); 19 } 20 catch (error) { 21 printError(error); 22 } 23} 24 25fetchAPOD();

响应字段类型说明
url字符串图片链接
explanation字符串图片说明
title字符串图片标题
date字符串该图片在NASA网站上展示的日期
1const API_KEY = '{{API_KEY}}'; 2 3const endpointUrl = new URL('https://api.nasa.gov/planetary/apod'); 4 5const queryParameters = new URLSearchParams({ 6 api_key: API_KEY, 7 start_date: '1997-10-03', 8 end_date: '1997-10-03' 9}); 10 11const options = { 12 method: 'GET' 13}; 14 15async function fetchAPOD() { 16 try { 17 endpointUrl.search = queryParameters; 18 const response = await fetch(endpointUrl, options); 19 printResponse(response); 20 } 21 catch (error) { 22 printError(error); 23 } 24} 25 26fetchAPOD();

地球和火星

NASA 图像与视频资料库