Typecho 自动友链管理插件,基于原 Links 插件的数据表,提供 API 接口实现友链的远程增删改查。博客的这篇文章其实就是README.md
typecho_links 数据表)重要: 插件目录名必须为 PALAutoLinks(不含连字符),否则 Typecho 1.2.1 无法加载。PALAutoLinks(必须!Typecho 1.2.1 的 namespace 机制不支持目录名含连字符)PALAutoLinks 文件夹上传至 Typecho 的 /usr/plugins/ 目录本插件不创建额外数据表,直接使用原 Links 插件的 typecho_links 表。请确保 Links 插件已启用或该表已存在。在插件设置页面中设置 API 访问令牌,这是调用 API 的唯一凭证。
| 配置项 | 说明 |
|---|---|
| API Token | 调用 API 时必须在请求参数中携带,建议使用 16 位以上随机字符串 |
API 接口地址为:https://你的域名/action/pal-api
https://你的域名/action/pal-apitoken(必填,值为你在后台设置的访问令牌)do(指定操作类型)| code | 含义 |
|---|---|
| 0 | 操作成功 |
| 1 | Token 验证失败 |
| 2 | 参数错误(缺少必填参数等) |
| 3 | 资源不存在(如链接 ID 找不到) |
| 4 | 数据库错误 |
获取全部友链,可按分类筛选。
请求参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| do | string | 是 | 固定值 list |
| token | string | 是 | API 访问令牌 |
| sort | string | 否 | 按分类筛选,不传则返回全部 |
成功响应示例:
{
"code": 0,
"msg": "success",
"data": [
{
"lid": "1",
"name": "示例博客",
"url": "https://example.com",
"sort": "friend",
"image": "",
"description": "一个示例博客",
"user": "",
"order": "1"
}
]
}请求参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| do | string | 是 | 固定值 add |
| token | string | 是 | API 访问令牌 |
| name | string | 是 | 链接名称 |
| url | string | 是 | 链接地址(需含 http(s)://) |
| sort | string | 否 | 链接分类 |
| image | string | 否 | 链接图片 URL |
| description | string | 否 | 链接描述 |
| user | string | 否 | 自定义数据 |
lid和order由系统自动生成,无需传入。
成功响应示例:
{
"code": 0,
"msg": "success",
"data": {
"lid": 1,
"name": "新博客",
"url": "https://newblog.com",
"sort": "friend",
"image": "",
"description": "",
"user": "",
"order": 1
}
}请求参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| do | string | 是 | 固定值 update |
| token | string | 是 | API 访问令牌 |
| lid | int | 是 | 链接 ID |
| name | string | 否 | 链接名称 |
| url | string | 否 | 链接地址 |
| sort | string | 否 | 链接分类 |
| image | string | 否 | 链接图片 URL |
| description | string | 否 | 链接描述 |
| user | string | 否 | 自定义数据 |
| order | int | 否 | 排序值 |
只传需要更新的字段即可,未传的字段保持原值不变。
成功响应示例:
{
"code": 0,
"msg": "updated",
"data": {
"lid": "1",
"name": "已更新名称",
"url": "https://updated.com",
"sort": "friend",
"image": "",
"description": "",
"user": "",
"order": "1"
}
}请求参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| do | string | 是 | 固定值 delete |
| token | string | 是 | API 访问令牌 |
| lid | int | 是 | 要删除的链接 ID |
成功响应示例:
{
"code": 0,
"msg": "deleted",
"data": null
}# 查询全部友链
curl "https://你的域名/action/pal-api?do=list&token=你的令牌"
# 按分类筛选
curl "https://你的域名/action/pal-api?do=list&token=你的令牌&sort=friend"
# 添加友链
curl -X POST "https://你的域名/action/pal-api" \
-d "do=add&token=你的令牌&name=我的博客&url=https://myblog.com&sort=friend&description=欢迎来访"
# 更新友链
curl -X POST "https://你的域名/action/pal-api" \
-d "do=update&token=你的令牌&lid=1&name=新名称"
# 删除友链
curl -X POST "https://你的域名/action/pal-api" \
-d "do=delete&token=你的令牌&lid=1"$apiUrl = 'https://你的域名/action/pal-api';
$token = '你的令牌';
// 查询
$response = file_get_contents("$apiUrl?do=list&token=$token");
$result = json_decode($response, true);
// 添加
$data = http_build_query([
'do' => 'add',
'token' => $token,
'name' => '新博客',
'url' => 'https://newblog.com',
'sort' => 'friend',
]);
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data,
]
]);
$response = file_get_contents($apiUrl, false, $context);
$result = json_decode($response, true);import requests
api_url = 'https://你的域名/action/pal-api'
token = '你的令牌'
# 查询
resp = requests.get(api_url, params={'do': 'list', 'token': token})
print(resp.json())
# 添加
resp = requests.post(api_url, data={
'do': 'add', 'token': token,
'name': '新博客', 'url': 'https://newblog.com', 'sort': 'friend',
})
print(resp.json())
# 更新
resp = requests.post(api_url, data={
'do': 'update', 'token': token, 'lid': 1, 'name': '新名称',
})
print(resp.json())
# 删除
resp = requests.post(api_url, data={
'do': 'delete', 'token': token, 'lid': 1,
})
print(resp.json())本插件使用原 Links 插件的 typecho_links 表(前缀 typecho_ 为默认值,实际以站点配置为准):
| 字段 | 类型 | 说明 |
|---|---|---|
| lid | INT / INTEGER | 主键,自增 |
| name | VARCHAR(200) | 链接名称 |
| url | VARCHAR(200) | 链接地址 |
| sort | VARCHAR(200) | 链接分类 |
| image | VARCHAR(200) | 链接图片 URL |
| description | VARCHAR(200) | 链接描述 |
| user | VARCHAR(200) | 自定义数据 |
| order | INT | 排序值,数值越小越靠前 |
Q: 调用 API 返回 {"code":1,"msg":"Token verification failed"} 怎么办?
A: 请检查后台插件设置中是否正确配置了访问令牌,以及请求参数中的 token 是否与设置值完全一致。
Q: 提示「Link not found」?
A: 请检查传入的 lid 是否正确。可以使用 list 操作查看所有链接及其 ID。
Q: 是否需要先安装原 Links 插件?
A: 需要有 typecho_links 数据表。可以先安装原 Links 插件让其自动建表,或者手动创建该表。
Q: 为什么插件目录名必须是 PALAutoLinks?
A: Typecho 1.2.1 的 Plugin::portal() 方法直接将目录名用于 PHP namespace(如 \Plugin\PALAutoLinks\Plugin),而 PHP namespace 不能包含连字符 -。原始目录名 PAL-PAL-Auto-Links 含连字符会导致类加载失败。
Q: 支持同时操作多条记录吗?
A: 目前每个请求只能操作一条记录。如需批量操作,请多次调用 API。