<?php
$API_KEY = "your_api_key"; // 请替换为您的API Key
$API_SECRET = "your_api_secret"; // 请替换为您的API Secret
$BASE_URL = "https://api.catfee.io";
// 生成当前的时间戳(ISO 8601格式)
function generateTimestamp() {
return gmdate("Y-m-d\TH:i:s.000\Z");
}
// 构建请求路径,包括查询参数
function buildRequestPath($path, $queryParams) {
if (empty($queryParams)) {
return $path;
}
$queryString = http_build_query($queryParams);
return $path . '?' . $queryString;
}
// 使用 HMAC-SHA256 算法生成签名
function generateSignature($timestamp, $method, $requestPath) {
$signString = $timestamp . $method . $requestPath;
return base64_encode(hash_hmac('sha256', $signString, $GLOBALS['API_SECRET'], true));
}
// 创建 HTTP 请求
function createRequest($url, $method, $timestamp, $signature) {
$headers = [
"Content-Type: application/json",
"CF-ACCESS-KEY: " . $GLOBALS['API_KEY'],
"CF-ACCESS-SIGN: " . $signature,
"CF-ACCESS-TIMESTAMP: " . $timestamp
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch (strtoupper($method)) {
case "POST":
curl_setopt($ch, CURLOPT_POST, true);
break;
case "GET":
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
break;
case "DELETE":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
throw new Exception("Unsupported HTTP method: $method");
}
$response = curl_exec($ch);
// 检查是否请求成功
if (curl_errno($ch)) {
throw new Exception("cURL error: " . curl_error($ch));
}
curl_close($ch);
return $response;
}
function main() {
$method = "POST"; // 可以修改为 "GET", "PUT", "DELETE" 等方法
$path = "/v1/order";
// 示例:创建订单
$queryParams = [
"quantity" => "65000",
"receiver" => "TRON_ADDRESS",
"duration" => "1h"
];
// 生成请求头
$timestamp = generateTimestamp();
$requestPath = buildRequestPath($path, $queryParams);
$signature = generateSignature($timestamp, $method, $requestPath);
// 创建请求 URL
$url = $BASE_URL . $requestPath;
// 发送请求
try {
$response = createRequest($url, $method, $timestamp, $signature);
echo "Response Code: 200\n";
echo "Response Body: $response\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
// 执行主函数
main();
?>