接入教程
1. 访问Petfinder开发者网站注册账号
2. 创建应用获取API密钥
3. 查阅API文档了解端点
4. 调用搜索接口获取宠物列表
5. 集成到应用中展示领养信息
获取宠物列表
python
import requests
base_url = 'https://api.petfinder.com/v2'
api_key = 'YOUR_API_KEY'
headers = {
'Authorization': f'Bearer {api_key}'
}
response = requests.get(f'{base_url}/animals', headers=headers)
if response.status_code == 200:
pets = response.json()
print('Retrieved pet list:', pets)
else:
print('Error:', response.status_code)
搜索宠物类型
php
<?php
$base_url = 'https://api.petfinder.com/v2';
$api_key = 'YOUR_API_KEY';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url . '/types');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $api_key
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
echo 'Error: ' . curl_error($ch);
} else {
$data = json_decode($response, true);
echo 'Pet types retrieved: ' . print_r($data, true);
}
curl_close($ch);
?>
获取单个宠物详情
javascript
const baseUrl = 'https://api.petfinder.com/v2';
const apiKey = 'YOUR_API_KEY';
async function getPetDetails(petId) {
const response = await fetch(`${baseUrl}/animals/${petId}`, {
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
if (response.ok) {
const petData = await response.json();
console.log('Pet details:', petData);
} else {
console.error('Error:', response.status);
}
}
getPetDetails(123);
常见问题
如何获取API密钥?
您需要在Petfinder开发者网站注册账户并创建一个应用程序来获取API密钥。具体步骤请访问官方网站的开发者文档部分。
API调用有频率限制吗?
是的,Petfinder API有调用频率限制以保障服务稳定性。具体限制请查阅官方文档中的速率限制部分。
可以获取哪些宠物信息?
API提供宠物基本信息,如品种、年龄、性别、健康状况、照片以及所在救助机构信息等。
Aitishiku.com