1. 概述
API 基础 URL:{域名}/index.php/api/
认证方式
所有 API 请求需在 Header 中携带 API Key,支持以下两种方式:
Authorization: Bearer {api_key}
X-API-Key: {api_key}
响应格式
{
"code": 0,
"msg": "success",
"data": {}
}
| 字段 | 类型 | 说明 |
|---|---|---|
| code | int | 状态码:0=成功, 401=认证失败, 403=权限不足, 500=业务错误 |
| msg | string | 提示信息 |
| data | object/array | 返回数据 |
2. 权限说明
每个 API Key 对应一组权限,由管理员在后台分配:
| 权限字段 | 值 | 说明 |
|---|---|---|
| can_write_own | 0 或 1 | 能否创建和编辑通过 API 创建的数据 |
| can_write_other | 0 或 1 | 能否编辑通过后台管理创建的数据 |
| can_delete | 0 / 1 / 2 | 0=不能删除, 1=仅删除 API 创建的数据, 2=可删除所有数据 |
权限组合示例
| 组合 | can_write_own | can_write_other | can_delete |
|---|---|---|---|
| 只读 | 0 | 0 | 0 |
| 只管自己 | 1 | 0 | 1 |
| 全权限 | 1 | 1 | 2 |
3. 文章接口
GET /api/articles/index - 文章列表
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | int | 否 | 页码,默认 1 |
| limit | int | 否 | 每页数量,默认 15 |
| type | string | 否 | 文章类型 |
| category_id | int | 否 | 分类 ID |
| keyword | string | 否 | 搜索关键词 |
请求示例
curl -X GET \
"https://example.com/index.php/api/articles/index?page=1&limit=10" \
-H "Authorization: Bearer your_api_key"
返回示例
{
"code": 0,
"msg": "success",
"data": {
"list": [
{
"id": 1,
"title": "文章标题",
"content": "文章内容",
"content_type": "html",
"type": "1",
"status": 1,
"source": "api",
"cover_text": "",
"create_time": 1700000000,
"update_time": 1700000000,
"categorys": [...],
"tags": [...]
}
],
"total": 100,
"page": 1
}
}
GET /api/articles/read - 文章详情
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | int | 是 | 文章 ID |
请求示例
curl -X GET \
"https://example.com/index.php/api/articles/read?id=1" \
-H "Authorization: Bearer your_api_key"
返回示例
{
"code": 0,
"msg": "success",
"data": {
"post": {
"id": 1,
"title": "文章标题",
"content": "文章内容",
"content_html": "<p>HTML内容</p>",
"content_type": "html",
"desc": "摘要",
"cover_text": "封面文案内容示例",
"poster": "/uploads/poster.jpg",
"type": "1",
"status": 1,
"source": "api"
},
"categories": [...],
"tags": [...]
}
}
POST /api/articles/save - 创建文章
Content-Type: application/json
通过此接口创建的文章会自动标记 source='api'
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| title | string | 是 | 文章标题 |
| content | string | 否 | 文章内容(Markdown) |
| content_html | string | 否 | 文章内容(HTML) |
| desc | string | 否 | 文章摘要 |
| cover_text | string | 否 | 封面文案,用于手机图片排版封面展示 |
| poster | string | 否 | 封面图 URL |
| type | string | 否 | 文章类型,默认 "1" |
| status | int | 否 | 状态,默认 0(草稿) |
| content_type | string | 否 | 内容类型: "html"(默认) 或 "markdown" 当为 "markdown" 时,系统会自动将 content 转换为 HTML 存储到 content_html 字段 |
| categorys | array | 否 | 分类 ID 数组,如 [1, 2] |
| tags | array | 否 | 标签 ID 数组,如 [1, 2] |
| publish_time | string | 否 | 发布时间,格式为 Y-m-d H:i:s,如 2024-01-15 14:30:00;当 status=1 且未传或传空字符串时,系统自动使用当前时间 |
注:create_time 和 update_time 由系统自动生成,客户端无需设置也无法修改。
请求示例(HTML)
curl -X POST \
"https://example.com/index.php/api/articles/save" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"title": "新文章标题",
"content": "文章内容",
"status": 1,
"publish_time": "2024-01-15 14:30:00",
"categorys": [1, 2],
"tags": [3]
}'
请求示例(Markdown)
curl -X POST \
"https://example.com/index.php/api/articles/save" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"title": "Markdown 文章",
"content": "# 标题\n\n段落内容",
"content_type": "markdown",
"type": "1",
"status": 1,
"categorys": [1, 2],
"tags": [3]
}'
返回示例
{
"code": 0,
"msg": "创建成功",
"data": {
"id": 42,
"uid": "6501a2b3c4d5e"
}
}
POST /api/articles/update - 编辑文章
Content-Type: application/json
权限规则:
source='api'的文章:需要can_write_own=1source='admin'的文章:需要can_write_other=1
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | int | 是 | 文章 ID |
| title | string | 否 | 文章标题 |
| content | string | 否 | 文章内容(Markdown) |
| content_html | string | 否 | 文章内容(HTML) |
| desc | string | 否 | 文章摘要 |
| cover_text | string | 否 | 封面文案,用于手机图片排版封面展示 |
| poster | string | 否 | 封面图 URL |
| status | int | 否 | 状态 |
| content_type | string | 否 | 内容类型: "html"(默认) 或 "markdown" 当为 "markdown" 时,系统会自动将 content 转换为 HTML 存储到 content_html 字段 |
| categorys | array | 否 | 分类 ID 数组(全量覆盖) |
| tags | array | 否 | 标签 ID 数组(全量覆盖) |
| publish_time | string | 否 | 发布时间,格式为 Y-m-d H:i:s,如 2024-01-15 14:30:00 |
注:create_time 和 update_time 由系统自动生成,客户端无需设置也无法修改。
请求示例
curl -X POST \
"https://example.com/index.php/api/articles/update" \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"id": 42,
"title": "修改后的标题",
"status": 1
}'
返回示例
{
"code": 0,
"msg": "更新成功",
"data": []
}
POST /api/articles/delete - 删除文章
权限规则:
source='api'的文章:需要can_delete >= 1source='admin'的文章:需要can_delete = 2
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | int | 是 | 文章 ID |
请求示例
curl -X POST \
"https://example.com/index.php/api/articles/delete" \
-H "Authorization: Bearer your_api_key" \
-d "id=42"
返回示例
{
"code": 0,
"msg": "删除成功",
"data": []
}
4. 附件接口
GET /api/attachments/index - 附件列表
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| page | int | 否 | 页码,默认 1 |
| limit | int | 否 | 每页数量,默认 20 |
| type | string | 否 | 附件类型 |
请求示例
curl -X GET \
"https://example.com/index.php/api/attachments/index?page=1&limit=10" \
-H "Authorization: Bearer your_api_key"
返回示例
{
"code": 0,
"msg": "success",
"data": {
"list": [
{
"id": 1,
"name": "image.png",
"save_name": "20260401_abc.png",
"url": "/uploads/20260401_abc.png",
"type": "image",
"size": 102400,
"source": "api",
"create_time": 1700000000
}
],
"total": 50,
"page": 1
}
}
POST /api/attachments/upload - 上传附件
Content-Type: multipart/form-data
上传的附件会自动标记 source='api'
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| file | file | 是 | 上传的文件 |
请求示例
curl -X POST \
"https://example.com/index.php/api/attachments/upload" \
-H "Authorization: Bearer your_api_key" \
-F "file=@/path/to/image.png"
返回示例
{
"code": 0,
"msg": "上传成功",
"data": {
"id": 10,
"name": "image.png",
"url": "/uploads/20260401_abc.png",
"size": 102400
}
}
POST /api/attachments/delete - 删除附件
权限规则:
source='api'的附件:需要can_delete >= 1source='admin'的附件:需要can_delete = 2
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | int | 是 | 附件 ID |
请求示例
curl -X POST \
"https://example.com/index.php/api/attachments/delete" \
-H "Authorization: Bearer your_api_key" \
-d "id=10"
返回示例
{
"code": 0,
"msg": "删除成功",
"data": []
}
5. 权限查询接口
GET /api/api_key_info/info - 查询当前 Key 权限
查询当前 API Key 的权限信息,无需额外参数。
请求示例
curl -X GET \
"https://example.com/index.php/api/api_key_info/info" \
-H "Authorization: Bearer your_api_key"
返回示例
{
"code": 0,
"msg": "success",
"data": {
"admin_id": 1,
"can_write_own": 1,
"can_write_other": 0,
"can_delete": 1,
"permissions_text": {
"can_write_own": "可管理自己的数据",
"can_write_other": "不可管理后台数据",
"can_delete": "仅删除API数据"
}
}
}
6. 错误码
| 错误码 | 说明 |
|---|---|
| 0 | 成功 |
| 401 | 认证失败(无效或已禁用的 API Key) |
| 403 | 权限不足 |
| 500 | 业务错误(参数缺失、资源不存在等) |