接入教程
1. 访问官网查看API文档
2. 获取API密钥并配置请求头
3. 使用标签参数筛选想要的图片类型
4. 调用接口获取图片数据
5. 解析返回的JSON数据并显示图片
6. 根据需求调整请求参数优化结果
Python 获取随机二次元图片
python
import requests
url = 'https://api.waifu.im/random'
headers = {'Authorization': 'Bearer YOUR_API_KEY'}
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
print(f'图片URL: {data["images"][0]["url"]}')
except requests.exceptions.RequestException as e:
print(f'请求失败: {e}')
PHP 通过标签搜索图片
php
<?php
$url = 'https://api.waifu.im/search';
$params = ['included_tags' => 'maid', 'limit' => 5];
$headers = ['Authorization: Bearer YOUR_API_KEY'];
$ch = curl_init($url . '?' . http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
echo '请求失败: ' . curl_error($ch);
} else {
$data = json_decode($response, true);
echo '第一张图片: ' . $data['images'][0]['url'];
}
curl_close($ch);
?>
JavaScript 获取多张热门图片
javascript
const url = 'https://api.waifu.im/popular';
const options = {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
};
fetch(url, options)
.then(response => {
if (!response.ok) throw new Error('网络响应错误');
return response.json();
})
.then(data => {
console.log(`获取到 ${data.images.length} 张图片`);
data.images.forEach(img => console.log(img.url));
})
.catch(error => console.error('请求失败:', error));
常见问题
如何获取API密钥?
请访问 Waifu.im 官方网站并注册账户,在用户仪表板中可以生成和管理您的API密钥。免费套餐通常包含一定的调用额度。
支持哪些图片标签?
Waifu.im 支持多种标签如 'maid'、'uniform'、'waifu'、'oppai' 等,完整标签列表请查阅官方文档的标签参考部分。
API调用有限制吗?
是的,根据您的套餐等级,API调用有速率限制和每日限额。免费用户通常每分钟可调用10-30次,具体限制请查看您的账户信息或官方定价页面。
Aitishiku.com