1. 普通表单提交
POST /api/generate.php HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
text=Hello+World&bg_color=%23FFFFFF&text_color=%23000000&font_size=20&width=800&height=600
2. JSON格式提交
POST /api/generate.php HTTP/1.1
Host: example.com
Content-Type: application/json
{
"text": "Hello World",
"bg_color": "#FFFFFF",
"text_color": "#000000",
"font_size": 20,
"width": 800,
"height": 600
}
3. Multipart 表单提交
POST /api/generate.php HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="text"
Hello World
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="bg_color"
#FFFFFF
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="text_color"
#000000
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="font_size"
20
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="width"
800
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="height"
600
------WebKitFormBoundary7MA4YWxkTrZu0gW--
curl -X POST "http://example.com/api/generate.php" \
-d "text=Hello World" \
-d "bg_color=#FFFFFF" \
-d "text_color=#000000" \
-d "font_size=20" \
-d "width=800" \
-d "height=600"
$data = array(
'text' => 'Hello World',
'bg_color' => '#FFFFFF',
'text_color' => '#000000',
'font_size' => 20,
'width' => 800,
'height' => 600
);
$ch = curl_init('http://example.com/api/generate.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
const formData = new FormData();
formData.append('text', 'Hello World');
formData.append('bg_color', '#FFFFFF');
formData.append('text_color', '#000000');
formData.append('font_size', '20');
formData.append('width', '800');
formData.append('height', '600');
fetch('http://example.com/api/generate.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
console.log('图片URL:', data.image_url);
} else {
console.error('错误:', data.message);
}
});
成功响应
{
"success": true,
"image_url": "http://example.com/generated/image_1234567890_abcdef.png"
}
失败响应
{
"success": false,
"message": "文字内容不能为空"
}