接入教程
1. 访问Crossref REST API文档网站
2. 注册获取API密钥(如需高级功能)
3. 使用基本搜索端点进行简单查询
4. 添加参数如标题、作者、DOI等细化搜索
5. 解析返回的JSON数据获取元信息
6. 根据需求实现分页或过滤功能
使用Python搜索Crossref元数据
import requests
url = 'https://api.crossref.org/works'
params = {
'query': 'machine learning',
'rows': 5
}
headers = {
'User-Agent': 'YourAppName (mailto:your@email.com)',
'Accept': 'application/json'
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
print(f'找到 {data["message"]["total-results"]} 条结果')
for item in data['message']['items']:
print(item.get('title', ['无标题'])[0])
else:
print(f'请求失败: {response.status_code}')
使用PHP获取Crossref文章信息
<?php
$url = 'https://api.crossref.org/works';
$queryParams = [
'query' => 'artificial intelligence',
'rows' => 3
];
$fullUrl = $url . '?' . http_build_query($queryParams);
$options = [
'http' => [
'method' => 'GET',
'header' => "User-Agent: YourAppName (mailto:your@email.com)\r\n" .
"Accept: application/json\r\n"
]
];
$context = stream_context_create($options);
$response = file_get_contents($fullUrl, false, $context);
if ($response !== false) {
$data = json_decode($response, true);
echo '找到 ' . $data['message']['total-results'] . ' 条结果\n';
foreach ($data['message']['items'] as $item) {
echo $item['title'][0] . "\n";
}
} else {
echo '请求失败';
}
?>
JavaScript查询Crossref出版物
const url = 'https://api.crossref.org/works';
const params = new URLSearchParams({
query: 'renewable energy',
rows: 4
});
fetch(`${url}?${params}`, {
method: 'GET',
headers: {
'User-Agent': 'YourAppName (mailto:your@email.com)',
'Accept': 'application/json'
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(`找到 ${data.message['total-results']} 条结果`);
data.message.items.forEach(item => {
console.log(item.title ? item.title[0] : '无标题');
});
})
.catch(error => {
console.error('请求失败:', error);
});
常见问题
Crossref API需要API密钥吗?
Crossref Metadata API通常不需要API密钥即可进行基本查询,但要求在所有请求中设置合法的User-Agent头部(包含应用名称和联系方式),这是出于礼貌使用原则。对于高频访问,建议注册邮箱以获得更宽松的限制。
API返回的数据格式是什么?
API默认返回JSON格式的数据。响应结构包含'message'字段,其中包含查询结果总数(total-results)和条目列表(items)。每个条目包含标题、作者、DOI、出版日期等元数据字段。可以通过Accept头部请求其他格式。
如何提高搜索结果的准确性?
可以使用多个查询参数来细化搜索,例如:'query.bibliographic'用于全文搜索,'query.author'用于作者搜索,'query.title'用于标题搜索。同时可以使用'filter'参数按出版类型、年份、期刊等条件过滤,例如'filter=type:journal-article,from-pub-date:2020'。
Aitishiku.com