接入教程
1. 访问官方网站注册获取API密钥
2. 查阅文档了解端点结构和参数格式
3. 使用基础搜索端点测试数据检索功能
4. 根据需要调用详细作品信息或图像接口
5. 处理返回的JSON数据集成到您的应用中
使用Python搜索艺术品
import requests
# 搜索艺术品
url = 'https://api.artic.edu/api/v1/artworks/search'
params = {
'q': 'monet',
'fields': 'id,title,artist_display,date_display,image_id'
}
headers = {
'Authorization': 'Bearer YOUR_API_KEY'
}
response = requests.get(url, params=params, headers=headers)
data = response.json()
# 处理结果
if data.get('data'):
for artwork in data['data']:
print(f"Title: {artwork.get('title')}")
print(f"Artist: {artwork.get('artist_display')}")
print("---")
else:
print("No artworks found.")
使用PHP获取艺术品详情
<?php
// 获取特定艺术品详情
$artworkId = 129884;
$url = 'https://api.artic.edu/api/v1/artworks/' . $artworkId;
$options = [
'http' => [
'method' => 'GET',
'header' => "Authorization: Bearer YOUR_API_KEY\r\n"
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response !== false) {
$data = json_decode($response, true);
if (isset($data['data'])) {
$artwork = $data['data'];
echo "Title: " . $artwork['title'] . "\n";
echo "Artist: " . $artwork['artist_display'] . "\n";
echo "Date: " . $artwork['date_display'] . "\n";
echo "Medium: " . $artwork['medium_display'] . "\n";
} else {
echo "Artwork not found.\n";
}
} else {
echo "Failed to fetch data.\n";
}
?>
使用JavaScript列出艺术家作品
// 获取艺术家作品列表
const artistName = 'Vincent van Gogh';
const apiUrl = 'https://api.artic.edu/api/v1/artworks/search';
async function fetchArtistWorks() {
try {
const response = await fetch(
`${apiUrl}?q=${encodeURIComponent(artistName)}&fields=id,title,date_display,image_id&limit=5`,
{
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
}
);
const data = await response.json();
if (data.data && data.data.length > 0) {
console.log(`Found ${data.data.length} works by ${artistName}:`);
data.data.forEach(artwork => {
console.log(`- ${artwork.title} (${artwork.date_display})`);
});
} else {
console.log('No works found for this artist.');
}
} catch (error) {
console.error('Error fetching artist works:', error);
}
}
// 调用函数
fetchArtistWorks();
常见问题
如何获取API访问密钥?
访问芝加哥艺术学院API官方网站(https://api.artic.edu/docs/),注册开发者账户并申请API密钥。免费层提供基础的访问权限,适用于个人项目和教育用途。
API的请求频率限制是多少?
免费API密钥通常有每分钟30-60次的请求限制。具体限制请查阅官方文档,商业用途或高流量应用可能需要升级到付费计划。
可以获取艺术品的高清图片吗?
是的,API提供艺术品的数字图像访问。通过艺术品数据中的image_id字段,可以构建图像URL获取不同尺寸的图片,但需遵守官方的使用条款和版权规定。
Aitishiku.com