留言板 · 开放 API v1
本接口提供留言板的两个能力:读取留言列表(含站长回复)与发送留言。全部接口返回 UTF-8 编码的 JSON。
统一使用 code 判断结果:0 表示成功,非 0 表示失败。
// 成功
{
"code": 0,
"message": "ok",
"data": { }
}
// 失败
{
"code": 1001,
"message": "参数校验失败",
"data": null,
"errors": ["昵称请填写 1 - 30 个字。"]
}| 方法 | 路径 | 说明 | 密钥 |
|---|---|---|---|
| GET | /api/messages.php |
获取留言列表与回复,支持分页 | 不需要 |
| POST | /api/messages.php |
发送一条留言 | 需要 |
读取留言列表不需要密钥;发送留言必须携带密钥。密钥由站长在后台「站点设置 → 接口密钥」查看或重置,重置后旧密钥立即失效。
以下四种方式任选一种,按优先级从高到低匹配:
| 方式 | 写法 |
|---|---|
| 请求头(推荐) | X-Api-Key: 你的密钥 |
| Bearer 认证 | Authorization: Bearer 你的密钥 |
| 请求体字段 | api_key(JSON 或表单字段) |
| 查询参数 | ?key=你的密钥 |
按发布时间倒序返回留言,每条留言附带其公开回复。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
page | int | 否 | 页码,默认 1;超出总页数时自动取最后一页 |
per_page | int | 否 | 每页条数,默认取站点设置,范围 1 - 50 |
| 字段 | 类型 | 说明 |
|---|---|---|
total | int | 留言总数 |
page | int | 当前页码(已修正) |
per_page | int | 每页条数(已修正) |
pages | int | 总页数 |
items[] | array | 留言列表,见下 |
items[].id | int | 留言 ID |
items[].name | string | 昵称 |
items[].content | string | 留言内容 |
items[].ip | string|null | 提交 IP;后台关闭「显示 IP」时为 null |
items[].created_at | string | 提交时间,格式 Y-m-d H:i:s |
items[].replies[] | array | 站长回复,按时间升序;无回复时为 [] |
items[].replies[].id | int | 回复 ID |
items[].replies[].content | string | 回复内容 |
items[].replies[].created_at | string | 回复时间 |
{
"code": 0,
"message": "ok",
"data": {
"total": 12,
"page": 1,
"per_page": 10,
"pages": 2,
"items": [
{
"id": 12,
"name": "访客甲",
"content": "留言内容",
"ip": "1.2.3.4",
"created_at": "2026-12-02 10:00:00",
"replies": [
{
"id": 3,
"content": "站长回复",
"created_at": "2026-12-02 11:00:00"
}
]
}
]
}
}curl "https://y.126355.xyz/api/messages.php?page=1&per_page=10"fetch("https://y.126355.xyz/api/messages.php?page=1&per_page=10")
.then(r => r.json())
.then(res => {
if (res.code !== 0) throw new Error(res.message);
res.data.items.forEach(item => {
console.log(item.name, item.content, item.replies);
});
});提交一条留言。支持 application/json 与普通表单(application/x-www-form-urlencoded / multipart/form-data)。
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
name | string | 是 | 昵称,1 - 30 个字 |
email | string | 是 | 邮箱,需符合邮箱格式,最长 120 字(不会公开返回) |
content | string | 是 | 留言内容,不能为空,长度上限见后台设置(默认 1000 字) |
api_key | string | 否 | 密钥;也可用请求头携带,二选一 |
{
"code": 0,
"message": "ok",
"data": {
"id": 13,
"name": "访客甲",
"content": "你好",
"created_at": "2026-12-02 12:00:00"
}
}curl -X POST "https://y.126355.xyz/api/messages.php" \
-H "X-Api-Key: 你的密钥" \
-H "Content-Type: application/json" \
-d '{"name":"访客甲","email":"a@b.com","content":"你好"}'fetch("https://y.126355.xyz/api/messages.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Api-Key": "你的密钥"
},
body: JSON.stringify({
name: "访客甲",
email: "a@b.com",
content: "你好"
})
})
.then(r => r.json())
.then(res => {
if (res.code !== 0) throw new Error(res.message);
console.log("留言 ID", res.data.id);
});<?php
$ch = curl_init('https://y.126355.xyz/api/messages.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json', 'X-Api-Key: 你的密钥'],
CURLOPT_POSTFIELDS => json_encode([
'name' => '访客甲',
'email' => 'a@b.com',
'content' => '你好',
], JSON_UNESCAPED_UNICODE),
]);
$res = json_decode(curl_exec($ch), true);
echo $res['code'] === 0 ? $res['data']['id'] : $res['message'];| code | HTTP | 含义 |
|---|---|---|
0 | 200 / 201 | 成功 |
1001 | 400 | 参数校验失败,errors[] 给出具体原因 |
1002 | 429 | 同一 IP 一小时内留言已达上限(2 条) |
1003 | 400 | 命中违禁词且站点设置为「拒收」 |
1004 | 429 | 未满站点设置的「同一 IP 提交间隔」 |
2001 | 401 | 密钥缺失或无效 |
4001 | 405 | 请求方法不支持(仅 GET / POST,响应头带 Allow: GET, POST) |
5001 | 503 | 站点尚未安装完成 |
5002 | 500 | 服务暂不可用(数据库连接或设置读取失败) |
5003 | 500 | 留言保存失败 |
参数校验失败示例:
{
"code": 1001,
"message": "参数校验失败",
"data": null,
"errors": ["请填写有效的邮箱地址。"]
}1002;如站点设置了「提交间隔」,间隔内返回 1004。1003)或打码后发布。Cache-Control: no-store,请勿缓存列表结果。