接入教程
1. 访问官网注册获取API密钥
2. 查阅文档了解可用端点与参数
3. 使用GET请求测试藏品搜索接口
4. 解析JSON响应获取艺术品数据
5. 将数据集成到您的应用展示中
6. 遵循使用条款与速率限制
Python示例:获取藏品列表
import requests
base_url = 'https://api.collection.cooperhewitt.org/api/rest'
api_key = 'YOUR_API_KEY'
params = {
'method': 'cooperhewitt.objects.getList',
'access_token': api_key,
'page': 1,
'per_page': 10
}
response = requests.get(base_url, params=params)
data = response.json()
if response.status_code == 200:
print(f'Successfully fetched {len(data.get("objects", []))} items')
for item in data.get('objects', []):
print(f"Title: {item.get('title')}")
else:
print(f'Error: {response.status_code}')
print(data.get('error', 'Unknown error'))
PHP示例:按关键词搜索藏品
<?php
$baseUrl = 'https://api.collection.cooperhewitt.org/api/rest';
$apiKey = 'YOUR_API_KEY';
$keyword = 'chair';
$queryParams = [
'method' => 'cooperhewitt.search.objects',
'access_token' => $apiKey,
'query' => $keyword,
'page' => 1,
'per_page' =>156
];
$url = $baseUrl . '?' . http_build_query($queryParams);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 200) {
$data = json_decode($response, true);
echo 'Found ' . count($data['objects'] ?? []) . ' results for "' . $keyword . '"' . PHP_EOL;
foreach ($data['objects'] ?? [] as $item) {
echo 'Item: ' . ($item['title'] ?? 'N/A') . PHP_EOL;
}
} else {
echo 'Request failed with status: ' . $httpCode . PHP_EOL;
$errorData = json_decode($response, true);
echo 'Error: ' . ($errorData['error'] ?? 'Unknown') . PHP_EOL;
}
?>
JavaScript示例:获取特定藏品详情
const baseUrl = 'https://api.collection.cooperhewitt.org/api/rest';
const apiKey = 'YOUR_API_KEY';
const objectId = '12345';
const params = new URLSearchParams({
method: 'cooperhewitt.objects.getInfo',
access_token: apiKey,
object_id: objectId
});
fetch(`${baseUrl}?${params}`)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log('Object Details:');
console.log(`Title: ${data.object?.title || 'N/A'}`);
console.log(`Date: ${data.object?.date || 'N/A'}`);
console.log(`Medium: ${data.object?.medium || 'N/A'}`);
if (data.object?.images) {
console.log(`Number of images: ${data.object.images.length}`);
}
})
.catch(error => {
console.error('Fetch error:', error);
});
常见问题
如何获取API密钥?
访问Cooper Hewitt官方网站的API页面(https://collection.cooperhewitt.org/api),通常需要注册账户并申请API密钥。密钥用于认证所有API请求。
API请求频率是否有限制?
是的,大多数公共API都设有速率限制以防止滥用。具体限制(如每分钟或每小时请求数)请查阅Cooper Hewitt API文档,通常在响应头中会包含剩余配额信息。
API返回的数据格式是什么?支持哪些语言?
API默认返回JSON格式的数据。目前主要支持英语元数据,部分藏品可能包含其他语言信息。响应结构遵循RESTful原则,包含状态码和标准化的错误信息。
Aitishiku.com