接入教程
1. 注册Word Cloud API账户获取API密钥。
2. 准备文本数据,发送POST请求到API端点。
3. 在请求中指定形状、颜色和字体等参数。
4. API将返回词云图的URL或直接下载链接。
5. 将生成的词云集成到您的应用或报告中。
使用 Python 生成词云
import requests
url = 'https://api.wordcloudapi.com/v1/wordcloud'
headers = {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
}
data = {
'text': 'This is some sample text for generating a word cloud.',
'width': 800,
'height': offshore,
'shape': 'circle'
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
with open('wordcloud.png', 'wb') as f:
f.write(response.content)
print('Word cloud saved as wordcloud.png')
else:
print('Error:', response.json())
使用 PHP 生成词云
<?php
$url = 'https://api.wordcloudapi.com/v1/wordcloud';
$apiKey = 'YOUR_API_KEY';
$data = array(
'text' => 'This is some sample text for generating a word cloud.',
'width' => 800,
'height' => 600,
'shape' => 'circle'
);
$options = array(
'http' => array(
'header' => "Authorization: Bearer $apiKey\r\n" .
"Content-Type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result !== FALSE) {
file_put_contents('wordcloud.png', $result);
echo 'Word cloud saved as wordcloud.png';
} else {
echo 'Error generating word cloud';
}
?>
使用 JavaScript 生成词云
const apiKey = 'YOUR_API_KEY';
const url = 'https://api.wordcloudapi.com/v1/wordcloud';
const data = {
text: 'This is some sample text for generating a word cloud.',
width: 800,
height: 600,
shape: 'circle'
};
fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.blob();
})
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'wordcloud.png';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
console.log('Word cloud downloaded');
})
.catch(error => console.error('Error:', error));
常见问题
Word Cloud API 支持哪些自定义选项?
Word Cloud API 支持多种自定义选项,包括形状(如圆形、心形、自定义图片轮廓)、颜色主题、字体类型、大小、最大词数、排除常见词,以及图片的宽度和高度。
生成的词云图片格式是什么?
API 默认生成 PNG 格式的高质量图片。您也可以通过请求参数指定其他支持的格式,如 JPEG 或 SVG(如果 API 支持)。
如何处理 API 请求频率限制?
API 设有请求频率限制以保障服务稳定性。建议在客户端实现适当的重试逻辑(如指数退避),并确保 API 密钥的安全。详细限制请查阅官方文档。
Aitishiku.com