获取授权密钥
在管理后台添加产品并生成授权密钥,记录下产品代码和授权密钥。
本文档详细说明了如何对接PHP授权验证系统的API接口。系统提供两个核心API:
在管理后台添加产品并生成授权密钥,记录下产品代码和授权密钥。
下载LicenseClient.php文件,放到您的项目目录中。
// 下载地址 https://shouquan.6vps.net/client/LicenseClient.php
<?php
require_once 'LicenseClient.php';
LicenseClient::init([
'server_url' => 'https://shouquan.6vps.net',
'product_code' => 'your-product-code',
'license_key' => 'YOUR-LICENSE-KEY',
'api_secret' => 'your-secret-key',
]);
$result = LicenseClient::verify();
if ($result['status']) {
echo "授权验证成功!";
} else {
echo "授权验证失败:" . $result['msg'];
}
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| license_key | string | 是 | 授权密钥 |
| product_code | string | 是 | 产品代码 |
| domain | string | 否 | 当前域名(用于域名验证) |
| ip | string | 否 | 当前IP地址(用于IP验证) |
| timestamp | int | 是 | 当前时间戳(防重放攻击) |
curl -X POST https://shouquan.6vps.net/api/verify \ -d "license_key=TEST-1234-ABCD-5678" \ -d "product_code=test-product" \ -d "domain=localhost" \ -d "timestamp=$(date +%s)"
{
"code": 200,
"msg": "验证成功",
"data": {
"license_id": 1,
"license_type": "professional",
"expire_time": null,
"product_version": "1.0.0",
"features": ["basic", "standard", "advanced", "api"],
"timestamp": 1774085157
},
"sign": "feaab90fae02662d49513155de39e144"
}
{
"code": 403,
"msg": "域名未授权",
"data": null
}
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| license_key | string | 是 | 授权密钥 |
| function_name | string | 是 | 要调用的函数名 |
| params | json | 否 | 函数参数(JSON格式) |
| timestamp | int | 是 | 当前时间戳 |
curl -X POST https://shouquan.6vps.net/api/remote \
-d "license_key=TEST-1234-ABCD-5678" \
-d "function_name=calculate_price" \
-d 'params={"price":100,"quantity":5}' \
-d "timestamp=$(date +%s)"
{
"code": 200,
"msg": "调用成功",
"data": {
"result": 450
}
}
LicenseClient::init([
'server_url' => 'https://shouquan.6vps.net', // 服务器地址
'product_code' => 'your-product-code', // 产品代码
'license_key' => 'YOUR-LICENSE-KEY', // 授权密钥
'api_secret' => 'your-secret-key', // API密钥
'cache_time' => 3600, // 缓存时间(秒)
]);
$result = LicenseClient::verify();
if ($result['status']) {
// 验证成功
$license_type = $result['data']['license_type'];
$features = $result['data']['features'];
} else {
// 验证失败
echo $result['msg'];
}
$result = LicenseClient::callRemote('calculate_price', [
'price' => 100,
'quantity' => 5
]);
if ($result['status']) {
echo "计算结果:" . $result['data']['result'];
}
if (LicenseClient::hasFeature('advanced_feature')) {
// 有此功能权限
} else {
// 无此功能权限
}
$info = LicenseClient::getLicenseInfo();
echo "授权类型:" . $info['license_type'];
echo "过期时间:" . $info['expire_time'];
echo "功能列表:" . implode(', ', $info['features']);
| 错误码 | 说明 | 解决方案 |
|---|---|---|
| 400 | 参数错误 | 检查请求参数是否完整 |
| 401 | 签名验证失败 | 检查API密钥是否正确 |
| 403 | 授权无效 | 检查授权密钥、域名、IP是否匹配 |
| 404 | 授权不存在 | 检查授权密钥是否正确 |
| 410 | 授权已过期 | 续费或重新生成授权 |
| 500 | 服务器错误 | 联系技术支持 |
所有API请求都需要进行签名验证,签名算法如下:
// 1. 将所有参数按字母顺序排序 ksort($params); // 2. 拼接成字符串 $sign_str = http_build_query($params); // 3. 加上API密钥 $sign_str .= $api_secret; // 4. 计算MD5 $sign = md5($sign_str);
每次请求都需要带上时间戳,服务器会验证时间戳是否在有效范围内(默认5分钟)。
所有API请求都使用HTTPS协议,确保数据传输安全。