Gindim 为开发者提供简单、高效的 API 服务接入方式。通过 AppKey + HMAC-SHA256 签名进行身份认证,按量计费。
所有 API 请求需要在 HTTP Header 中携带以下参数:
| Header | 类型 | 必填 | 说明 |
|---|---|---|---|
X-AppKey | string | 是 | 平台分配的 AppKey |
X-Timestamp | int | 是 | 当前 Unix 时间戳(秒),与服务器时间差不超过 5 分钟 |
X-Nonce | string | 是 | 随机字符串,每次请求不同,用于防重放 |
X-Sign | string | 是 | 签名:HMAC-SHA256(AppKey+Timestamp+Nonce, SecretKey) |
const crypto = require('crypto');
const appkey = 'ak_xxxxxxxxxxxxxxxx';
const secretKey = 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const baseUrl = 'https://你的域名/api/service/aj-captcha';
function signHeaders() {
const timestamp = Math.floor(Date.now() / 1000).toString();
const nonce = crypto.randomBytes(8).toString('hex');
const sign = crypto.createHmac('sha256', secretKey)
.update(appkey + timestamp + nonce)
.digest('hex');
return {
'X-AppKey': appkey,
'X-Timestamp': timestamp,
'X-Nonce': nonce,
'X-Sign': sign
};
}
// 获取验证码
const captcha = await fetch(baseUrl + '/get', { headers: signHeaders() }).then(r => r.json());
// 校验滑块偏移
const result = await fetch(baseUrl + '/check', {
method: 'POST',
headers: Object.assign({ 'Content-Type': 'application/json' }, signHeaders()),
body: JSON.stringify({
captchaId: captcha.data.captchaId,
sliderOffset: 120
})
}).then(r => r.json());
<?php
$appkey = 'ak_xxxxxxxxxxxxxxxx';
$secretKey = 'sk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$baseUrl = 'https://你的域名/api/service/aj-captcha';
function gindimSign(string $appkey, string $secretKey): array
{
$timestamp = (string) time();
$nonce = bin2hex(random_bytes(8));
$sign = hash_hmac('sha256', $appkey . $timestamp . $nonce, $secretKey);
return [
'X-AppKey: ' . $appkey,
'X-Timestamp: ' . $timestamp,
'X-Nonce: ' . $nonce,
'X-Sign: ' . $sign,
];
}
function gindimRequest(string $method, string $url, array $headers, ?string $body = null): array
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $body,
]);
$raw = curl_exec($ch);
curl_close($ch);
return json_decode($raw ?: '', true) ?: [];
}
$captcha = gindimRequest('GET', $baseUrl . '/get', gindimSign($appkey, $secretKey));
$payload = json_encode([
'captchaId' => $captcha['data']['captchaId'] ?? '',
'sliderOffset' => 120,
]);
$headers = gindimSign($appkey, $secretKey);
$headers[] = 'Content-Type: application/json';
$result = gindimRequest('POST', $baseUrl . '/check', $headers, $payload);
# cURL 示例
APPKEY="ak_xxx"
SECRET="sk_xxx"
TS=$(date +%s)
NONCE=$(openssl rand -hex 8)
SIGN=$(echo -n "${APPKEY}${TS}${NONCE}" | openssl dgst -sha256 -hmac "${SECRET}" | awk '{print $2}')
curl -H "X-AppKey: ${APPKEY}" \
-H "X-Timestamp: ${TS}" \
-H "X-Nonce: ${NONCE}" \
-H "X-Sign: ${SIGN}" \
https://api.gindim.com/service/aj-captcha/get
GET /api/service/aj-captcha/get
无需参数。返回 captchaId、底图(含缺口)、拼图块、Y 坐标。
POST /api/service/aj-captcha/check
| 参数 | 类型 | 说明 |
|---|---|---|
captchaId | string | 获取验证码时返回的 ID |
sliderOffset | int | 滑块偏移量(0~340 像素) |
GET /api/service/aj-captcha/info
返回服务基本信息与商户剩余调用次数。
| HTTP Code | 说明 |
|---|---|
| 200 | 请求成功 |
| 401 | 缺少认证参数或时间戳过期 |
| 402 | 调用次数不足,请购买套餐 |
| 403 | 签名验证失败或 AppKey 无效 |
| 404 | 服务不存在 |
1. 注册 Gindim 账号 → 2. 选购 API 服务套餐 → 3. 在控制台获取 AppKey/SecretKey → 4. 按上方示例签名调用