短链操作接口
用于创建、修改和更新短链接的核心接口,支持生成短链接、修改短链信息以及执行短链更新操作。
接口概览
POST
/api/shortlink_api.php
通过不同的action参数,该接口可以处理多种短链接操作。
🔐 认证说明
所有请求需要进行用户认证,使用专门的API密钥。系统已在users表中添加了api_key字段用于存储用户的API密钥。
通用参数
| 参数名 | 类型 | 必填 | 描述 |
|---|---|---|---|
| username | string | 是 | 用户名 |
| api_key | string | 是 | 用户的API密钥(存储在系统中的专门API密钥字段) |
| action | string | 是 | 操作类型:generate、update、execute_update |
1. 生成短链 (generate)
创建新的短链接,返回短链接的访问地址和相关信息。
请求参数
| 参数名 | 类型 | 必填 | 描述 |
|---|---|---|---|
| target_url | string | 是 | 目标URL地址 |
| title | string | 是 | 链接标题 |
| redirect_type | string | 否 | 跳转类型,默认值为'direct' |
| redirect_url_a | string | 否 | 重定向URL A |
请求示例
curl -X POST -H "Content-Type: application/json" -d '{
"username": "user123",
"api_key": "your_api_key_here",
"action": "generate",
"target_url": "https://www.example.com",
"title": "测试链接",
"redirect_type": "direct",
"redirect_url_a": "https://example.redirect.com/a"
}' http://your-domain.com/api/shortlink_api.php
成功响应
{"success": true,"data": {
"a_url": "https://example.com/abc123",
"b_url": "https://example2.com/def456",
"short_code": "abc123",
"remaining_balance": 49
}}
2. 修改短链 (update)
更新已存在的短链接信息,如目标URL和标题。
请求参数
| 参数名 | 类型 | 必填 | 描述 |
|---|---|---|---|
| link_id | integer | 是 | 短链ID |
| target_url | string | 是 | 新的目标URL |
| title | string | 否 | 新的链接标题 |
请求示例
curl -X POST -H "Content-Type: application/json" -d '{
"username": "user123",
"api_key": "your_api_key_here",
"action": "update",
"link_id": 123,
"target_url": "https://www.newexample.com"
}' http://your-domain.com/api/shortlink_api.php
成功响应
{"success": true,"data": {
"link_id": 123,
"message": "短链更新成功"
}}
3. 执行更新 (execute_update)
执行短链接的更新操作,通常用于刷新短链接的访问地址。
请求参数
| 参数名 | 类型 | 必填 | 描述 |
|---|---|---|---|
| link_id | integer | 是 | 短链ID |
请求示例
curl -X POST -H "Content-Type: application/json" -d '{
"username": "user123",
"api_key": "your_api_key_here",
"action": "execute_update",
"link_id": 123
}' http://your-domain.com/api/shortlink_api.php
成功响应
{"success": true,"data": {
"link_id": 123,
"new_a_url": "https://example.com/new123",
"new_b_url": "https://example2.com/new456",
"remaining_balance": 48,
"message": "短链执行更新成功"
}}
错误码说明
| HTTP状态码 | 错误信息 | 描述 |
|---|---|---|
| 400 | 缺少必要参数 | 请求缺少必要的参数 |
| 400 | 生成短链缺少必要参数 | 生成短链时缺少必要参数 |
| 400 | 修改短链缺少必要参数 | 修改短链时缺少必要参数 |
| 400 | 执行更新缺少必要参数 | 执行更新时缺少必要参数 |
| 400 | 账户已过期 | 用户账户已过期 |
| 400 | 生成次数不足 | 用户生成次数不足 |
| 400 | 余额不足,无法执行更新 | 用户余额不足,无法执行更新 |
| 400 | 未找到该短链或无权修改 | 找不到指定的短链或用户没有权限修改 |
| 400 | 无效的操作类型 | action参数值无效 |
| 401 | 认证失败,用户名或API密钥错误 | 用户认证失败 |
| 405 | 只支持POST请求 | 请求方法不是POST |
| 500 | 服务器内部错误 | 服务器处理请求时发生错误 |
注意事项
- 所有请求必须使用JSON格式提交数据。
- 生成和执行更新操作会消耗用户的生成次数(余额)。
- 请确保用户账户处于有效期内且有足够的余额。
- 操作短链时,请确保您拥有该短链的所有权。
- API密钥是重要凭证,请妥善保管,不要在客户端代码或公共场合暴露。
- 生成短链时,redirect_type和redirect_url_a为可选参数,具体支持的跳转类型请参考系统配置。