接入教程
1. 访问 https://placebear.com/ 了解基础用法
2. 在图片URL后添加尺寸参数,例如 /200/300 获取宽200高300的图片
3. 将生成的图片URL直接嵌入HTML的img标签中即可使用
使用 Python 获取占位熊图片
python
import requests
url = 'https://api.example.com/bear'
params = {
'width': 800,
'height':案例 600
}
headers = {
'Authorization': 'Bearer YOUR_API_KEY'
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
print('Image fetched successfully:', response.url)
else:
print('Failed to fetch image:', response.status_code)
使用 PHP 获取占位熊图片
php
<?php
$url = 'https://api.example.com/bear?' . http_build_query(['width' => 800, 'height' => 600]);
$options = [
'http' => [
'header' => "Authorization: Bearer YOUR_API_KEY\r\n"
]
];
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response !== false) {
echo 'Image fetched successfully.';
} else {
echo 'Failed to fetch image.';
}
?>
使用 JavaScript 获取占位熊图片
javascript
const url = 'https://api.example.com/bear';
const params = new URLSearchParams({
width: 800,
height: 600
});
fetch(`${url}?${params}`, {
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
})
.then(response => {
if (response.ok) {
console.log('Image fetched successfully:', response.url);
} else {
console.error('Failed to fetch image:', response.status);
}
})
.catch(error => console.error('Error:', error));
常见问题
PlaceBear API 是否需要注册才能使用?
不需要。PlaceBear API 是免费的,无需注册即可直接调用,只需在请求中指定尺寸等参数即可获取图片。
可以自定义图片的尺寸吗?
是的。您可以通过 width 和 height 参数自定义图片的宽度和高度,例如 width=800&height=600。
API 调用是否有频率限制?
PlaceBear API 目前没有严格的频率限制,但建议合理使用以避免对服务造成过大压力。
Aitishiku.com