版本: 1.0.0 | 更新时间: 2026-03-07
URL: ?action=generate
方法: GET
返回: PNG图片 + X-Captcha-Token响应头
| Header | 说明 |
|---|---|
| X-Captcha-Token | 验证码令牌(用于验证时提交) |
| Content-Type | image/png |
| Cache-Control | no-store, no-cache(防止缓存) |
// 生成验证码并获取token
var captchaToken = '';
var img = document.getElementById('captcha-img');
var timestamp = new Date().getTime();
fetch('http://captcha.example.com/?action=generate&t=' + timestamp)
.then(response => {
// 获取token
captchaToken = response.headers.get('X-Captcha-Token');
return response.blob();
})
.then(blob => {
// 显示图片
img.src = URL.createObjectURL(blob);
});
URL: ?action=verify
方法: POST / GET
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| token | string | 是 | 生成验证码时返回的token |
| code | string | 是 | 用户输入的验证码 |
{
"code": 200,
"success": true,
"message": "Verification successful"
}
{
"code": 400,
"success": false,
"message": "Verification failed"
}
// 验证验证码
var userCode = document.getElementById('captcha-input').value;
fetch('http://captcha.example.com/?action=verify', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'token=' + encodeURIComponent(captchaToken) +
'&code=' + encodeURIComponent(userCode)
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('验证成功');
} else {
alert('验证失败');
}
});
URL: ?action=test
方法: GET
返回: JSON格式的服务状态信息
编辑 config.php 文件进行配置:
$config = [
// CORS跨域配置
'allowed_origins' => [
'https://your-domain.com',
],
'allow_all_origins' => false, // 生产环境建议false
// 验证码配置
'captcha' => [
'length' => 5, // 验证码长度
'width' => 150, // 图片宽度
'height' => 50, // 图片高度
'expire' => 300, // 有效期(秒)
]
];
captcha_service 目录到服务器config.php 中的域名白名单storage 目录可写(chmod 755)http://your-domain.com/?action=test 测试allow_all_origins = false如有问题,请检查: