1. 生成短链 基础
POST
/api/main.php?method=url
请求参数
| 参数名 | 类型 | 必填 | 描述 |
| url | string | 是 | 长链接URL |
| short | string | 否 | 自定义短链(4-10位字母数字) |
| domain | string | 否 | 自定义域名 |
返回示例
请求
POST /api/main.php?method=url
url=https://example.com/page&short=custom1
成功响应
{
"code": 0,
"msg": "success",
"url": "https://yourdomain.com/custom1"
}
失败响应
{
"code": 1001,
"msg": "缺少URL参数"
}
3. 批量生成短链 高并发
POST
/api/main.php?method=batch
性能指标:支持单次批量生成最多5000条短链,秒级响应
请求参数
| 参数名 | 类型 | 必填 | 描述 |
| urls | array | 是 | URL数组,最多5000个元素(可传JSON字符串) |
| shorts | array | 否 | 自定义短链数组,与urls一一对应 |
| prefix | string | 否 | 短链前缀 |
| domain | string | 否 | 自定义域名,替换默认域名 |
| domain_group | string | 否 | 域名分组名称,从分组随机选域名(优先级高于domain) |
| skip_validation | boolean | 否 | 跳过URL验证,提高性能(默认false) |
返回示例
请求示例
POST /api/main.php?method=batch
Content-Type: application/x-www-form-urlencoded
urls=["https://example.com/1","https://example.com/2"]
&prefix=pre_
&skip_validation=true
&domain_group=group_a
成功响应
{
"code": 0,
"msg": "success",
"data": {
"total": 2,
"success_count": 2,
"fail_count": 0,
"duration_ms": 45.23,
"tps": 44.21,
"success": [
{
"index": 0,
"original_url": "https://example.com/1",
"short": "pre_a1b2c",
"short_url": "https://yourdomain.com/pre_a1b2c"
}
],
"failed": []
}
}
4. 域名列表 分组
GET/POST
/api/main.php?method=domain_list
请求参数
| 参数名 | 类型 | 必填 | 描述 |
| group | string | 否 | 分组名称,不传返回所有分组 |
| format | string | 否 | 返回格式:json(默认)或 select(仅返回域名数组) |
返回示例
获取所有分组域名
GET /api/main.php?method=domain_list
{
"code": 0,
"msg": "success",
"data": {
"groups": {
"group_a": [
{"domain": "a1.example.com", "target_url": "https://long.a.com"},
{"domain": "a2.example.com", "target_url": "https://long.a.com"}
],
"group_b": [
{"domain": "b1.example.com", "target_url": "https://long.b.com"}
]
},
"total": 3
}
}
获取指定分组的域名(select格式,直接用于下拉框)
GET /api/main.php?method=domain_list&group=group_a&format=select
{
"code": 0,
"msg": "success",
"data": ["a1.example.com", "a2.example.com"]
}
使用说明
1. 数据库配置
// 修改 api/config.php
const DB = array('数据库地址', '数据库名', '用户名', '密码');
2. 域名分组配置
执行 domain_migrate.sql 添加分组字段,然后插入数据:
INSERT INTO `domain_redirect` (`domain`, `target_url`, `time`, `ip`, `group_name`)
VALUES ('short.example.com', 'https://long.com', UNIX_TIMESTAMP(), '127.0.0.1', 'group_a');
3. Redis缓存(可选,提升性能)
// 修改 api/config.php
const REDIS_ENABLED = true;
const REDIS_HOST = '127.0.0.1';
const REDIS_PORT = 6379;
4. 客户端调用示例
参考 api_client_example.php 文件