{include file='common/_require'/}
{include file='common/_left'/}

1. 概述

API 基础 URL:{域名}/index.php/api/

认证方式

所有 API 请求需在 Header 中携带 API Key,支持以下两种方式:

Authorization: Bearer {api_key} X-API-Key: {api_key}

响应格式

{ "code": 0, "msg": "success", "data": {} }
字段类型说明
codeint状态码:0=成功, 401=认证失败, 403=权限不足, 500=业务错误
msgstring提示信息
dataobject/array返回数据

2. 权限说明

每个 API Key 对应一组权限,由管理员在后台分配:

权限字段说明
can_write_own0 或 1能否创建和编辑通过 API 创建的数据
can_write_other0 或 1能否编辑通过后台管理创建的数据
can_delete0 / 1 / 20=不能删除, 1=仅删除 API 创建的数据, 2=可删除所有数据

权限组合示例

组合can_write_owncan_write_othercan_delete
只读000
只管自己101
全权限112

3. 文章接口

GET /api/articles/index - 文章列表 所有有效 Key

请求参数

参数类型必填说明
pageint页码,默认 1
limitint每页数量,默认 15
typestring文章类型
category_idint分类 ID
keywordstring搜索关键词

请求示例

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 - 文章详情 所有有效 Key

请求参数

参数类型必填说明
idint文章 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 - 创建文章 can_write_own=1

Content-Type: application/json

通过此接口创建的文章会自动标记 source='api'

请求参数

参数类型必填说明
titlestring文章标题
contentstring文章内容(Markdown)
content_htmlstring文章内容(HTML)
descstring文章摘要
cover_textstring封面文案,用于手机图片排版封面展示
posterstring封面图 URL
typestring文章类型,默认 "1"
statusint状态,默认 0(草稿)
content_typestring内容类型: "html"(默认) 或 "markdown"
当为 "markdown" 时,系统会自动将 content 转换为 HTML 存储到 content_html 字段
categorysarray分类 ID 数组,如 [1, 2]
tagsarray标签 ID 数组,如 [1, 2]
publish_timestring发布时间,格式为 Y-m-d H:i:s,如 2024-01-15 14:30:00;当 status=1 且未传或传空字符串时,系统自动使用当前时间

注:create_timeupdate_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=1
  • source='admin' 的文章:需要 can_write_other=1

请求参数

参数类型必填说明
idint文章 ID
titlestring文章标题
contentstring文章内容(Markdown)
content_htmlstring文章内容(HTML)
descstring文章摘要
cover_textstring封面文案,用于手机图片排版封面展示
posterstring封面图 URL
statusint状态
content_typestring内容类型: "html"(默认) 或 "markdown"
当为 "markdown" 时,系统会自动将 content 转换为 HTML 存储到 content_html 字段
categorysarray分类 ID 数组(全量覆盖)
tagsarray标签 ID 数组(全量覆盖)
publish_timestring发布时间,格式为 Y-m-d H:i:s,如 2024-01-15 14:30:00

注:create_timeupdate_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 >= 1
  • source='admin' 的文章:需要 can_delete = 2

请求参数

参数类型必填说明
idint文章 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 - 附件列表 所有有效 Key

请求参数

参数类型必填说明
pageint页码,默认 1
limitint每页数量,默认 20
typestring附件类型

请求示例

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 - 上传附件 can_write_own=1

Content-Type: multipart/form-data

上传的附件会自动标记 source='api'

请求参数

参数类型必填说明
filefile上传的文件

请求示例

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 >= 1
  • source='admin' 的附件:需要 can_delete = 2

请求参数

参数类型必填说明
idint附件 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 权限 所有有效 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业务错误(参数缺失、资源不存在等)
{include file='common/_right'/}