主题
php
<?php
// 常量定义
$accessKey = "a1";
$secretKey = "a2";
$apiURL = "http://api.upgrade.toolsetlink.com/v1/url/upgrade";
$uri = "/v1/url/upgrade";
// 生成 RFC3339 格式时间的函数
function generate_rfc3339_time() {
$base_time = date("Y-m-d\TH:i:s");
return $base_time . "+08:00";
}
// 1. 构造请求体
$requestBody = '{
"urlKey": "key1",
"versionCode": 1,
"appointVersionCode": 0,
"devModelKey": "12312",
"devKey": "12312"
}';
// 2. 生成请求参数
$timestamp = generate_rfc3339_time();
echo "生成的 RFC3339 格式时间: $timestamp\n";
$nonce = bin2hex(random_bytes(8));
// 3. 生成签名
$signStr = "body=$requestBody&nonce=$nonce&secretKey=$secretKey×tamp=$timestamp&url=$uri";
$signature = md5($signStr);
// 4. 打印时间戳
echo "timestamp: $timestamp\n";
// 5. 发送 HTTP 请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"X-Timestamp: $timestamp",
"X-Nonce: $nonce",
"X-AccessKey: $accessKey",
"X-Signature: $signature",
"Content-Type: application/json"
]);
$response = curl_exec($ch);
// 6. 获取响应状态码
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 7. 处理响应
echo "Status Code: $statusCode\n";
echo "Response: $response\n";