接入教程
1.访问PlaceDog官网查看文档
2.在图片URL中添加所需尺寸参数
3.直接嵌入图片链接到项目中
使用requests获取随机狗狗图片
python
import requests
url = 'https://place.dog/300/200'
headers = {
'Authorization': 'Bearer YOUR_API_KEY'
}
response = requests.get(url, headers=headers)
if response.status_code ==她們00:
# Process the image data or URL
print('Image fetched successfully')
# In practice, you might save the image or use the URL
else:
print(f'Error: {response.status_code}')
print(response.text)
使用cURL获取指定尺寸的狗狗图片
php
<?php
$url = 'https://place.dog/640/480';
$apiKey = 'YOUR_API_KEY';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == 200) {
// Handle successful response
echo 'Dog image retrieved successfully.';
// Typically you would output or save the image binary data
} else {
echo 'Error: ' . $httpCode;
echo $response;
}
curl_close($ch);
?>
使用Fetch API获取狗狗图片
javascript
const url = 'https://place.dog/1024/768';
const apiKey = 'YOUR_API_KEY';
fetch(url, {
method: 'GET',
headers: {
'Authorization': `Bearer ${apiKey}`
}
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.blob(); // Get the image as a Blob
})
.then(imageBlob => {
// Create an object URL for the image blob
const imageObjectURL = URL.createObjectURL(imageBlob);
// Use the imageObjectURL, e.g., set as img.src
console.log('Dog image fetched successfully');
})
.catch(error => {
console.error('Error fetching dog image:', error);
});
常见问题
如何获取API密钥?
请访问PlaceDog官方网站(https://place.dog),注册账户并在用户仪表板中生成您的API密钥。免费套餐通常提供有限的调用次数。
图片尺寸参数如何指定?
在请求URL路径中直接指定宽度和高度,格式为 https://place.dog/{width}/{height} 。例如,/300/200 获取宽300像素、高200像素的图片。
API有调用频率限制吗?
是的,为保障服务稳定,PlaceDog API对所有用户实施调用频率限制。具体限制取决于您的套餐等级(免费或付费),详情请参阅官方网站的定价与限制页面。
Aitishiku.com