🔗 URL短链服务 API文档

高并发短链接生成服务 | 支持单条/批量生成 | 域名分组管理

1. 生成短链 基础

POST
/api/main.php?method=url

请求参数

参数名类型必填描述
urlstring长链接URL
shortstring自定义短链(4-10位字母数字)
domainstring自定义域名

返回示例

请求
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参数" }

2. 获取长链接 查询

GET
/api/main.php?method=geturl&short=xxx

请求参数

参数名类型必填描述
shortstring短链标识

返回示例

成功响应
{ "code": 0, "msg": "success", "url": "https://example.com/original-page" }

3. 批量生成短链 高并发

POST
/api/main.php?method=batch
性能指标:支持单次批量生成最多5000条短链,秒级响应

请求参数

参数名类型必填描述
urlsarrayURL数组,最多5000个元素(可传JSON字符串)
shortsarray自定义短链数组,与urls一一对应
prefixstring短链前缀
domainstring自定义域名,替换默认域名
domain_groupstring域名分组名称,从分组随机选域名(优先级高于domain)
skip_validationboolean跳过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

请求参数

参数名类型必填描述
groupstring分组名称,不传返回所有分组
formatstring返回格式: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"] }

状态码说明

状态码说明
0成功
1001参数错误(缺少参数或格式错误)
1002短链接格式错误(只能包含字母和数字)
1003短链接长度错误(必须在4-10位之间)
1005URL长度错误(必须在10-1500字符之间)
1006自定义短链接已存在
99999系统错误

使用说明

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 文件