接入教程
1. 访问FishWatch开发者网站获取API密钥
2. 查阅文档了解可用端点与参数
3. 调用物种端点获取基础信息
4. 使用图片端点下载鱼类图像
5. 根据需要处理返回的JSON数据
6. 集成到应用程序中实现功能
获取鱼类物种列表
python
import requests
url = "https://api.fishwatch.gov/api/species"
headers = {
"X-API-Key": "YOUR_API_KEY"
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
print(f"Found {len(data)} species")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
按名称查询鱼类详细信息
php
<?php
$apiKey = 'YOUR_API_KEY';
$speciesName = urlencode('Atlantic Salmon');
$url = "https://api.fishwatch.gov/api/species/{$speciesName}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"X-API-Key: {$apiKey}"
]);
$response = curl_exec($ch);
if ($response === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
$data = json_decode($response, true);
echo "Species: " . ($data['Species Name'] ?? 'Not found');
}
curl_close($ch);
?>
获取鱼类图片资源
javascript
const fetchFishImages = async (speciesId) => {
const url = `https://api.fishwatch.gov/api/species/${speciesId}/images`;
const options = {
method: 'GET',
headers: {
'X-API-Key': 'YOUR_API_KEY'
}
};
try {
const response = await fetch(url, options);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const images = await response.json();
console.log(`Retrieved ${images.length} image(s)`);
return images;
} catch (error) {
console.error('Fetch error:', error);
}
};
// Example usage
fetchFishImages('salmon-atlantic');
常见问题
如何获取API密钥?
请访问FishWatch开发者网站(https://www.fishwatch.gov/developers)注册账户并申请API密钥。通常需要提供使用目的和联系信息。
API调用频率有限制吗?
是的,FishWatch API设有速率限制以保障服务稳定。具体限制根据您的API密钥类型而定,免费版通常为每分钟60次请求。超出限制将收到429状态码。
API返回的数据格式是什么?
所有API端点均返回JSON格式数据,包含鱼类物种的学名、俗名、分布区域、生物特征、保护状态及图片链接等结构化信息。响应内容遵循统一的字段命名规范。
Aitishiku.com