Ensure that your PHP environment has the cURL extension installed (most PHP installations include it by default).
Example Code
<?php
$API_KEY = "your_api_key"; // Replace with your actual API Key
$API_SECRET = "your_api_secret"; // Replace with your actual API Secret
$BASE_URL = "https://api.catfee.io";
// Generate the current timestamp in ISO 8601 format
function generateTimestamp() {
return gmdate("Y-m-d\TH:i:s.000\Z");
}
// Build the request path, including query parameters
function buildRequestPath($path, $queryParams) {
if (empty($queryParams)) {
return $path;
}
$queryString = http_build_query($queryParams);
return $path . '?' . $queryString;
}
// Generate the HMAC-SHA256 signature
function generateSignature($timestamp, $method, $requestPath) {
$signString = $timestamp . $method . $requestPath;
return base64_encode(hash_hmac('sha256', $signString, $GLOBALS['API_SECRET'], true));
}
// Create and send the HTTP request
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);
// Check for errors
if (curl_errno($ch)) {
throw new Exception("cURL error: " . curl_error($ch));
}
curl_close($ch);
return $response;
}
function main() {
$method = "POST"; // Change to "GET", "PUT", or "DELETE" as needed
$path = "/v1/order";
// Example: Create order
$queryParams = [
"quantity" => "65000",
"receiver" => "TRON_ADDRESS",
"duration" => "1h"
];
$timestamp = generateTimestamp();
$requestPath = buildRequestPath($path, $queryParams);
$signature = generateSignature($timestamp, $method, $requestPath);
$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";
}
}
// Run the main function
main();
?>
Code Explanation
generateTimestamp():
Generates the current UTC timestamp in ISO 8601 format using gmdate("Y-m-d\TH:i:s.000\Z").
buildRequestPath():
Builds the request path including query parameters (if any), using http_build_query() for URL encoding. Note that the query string is not sorted.
generateSignature():
Concatenates timestamp + method + requestPath as the signing string, then uses hash_hmac() with HMAC-SHA256 and encodes the result with Base64.
createRequest():
Sends an HTTP request using the appropriate method (POST, GET, PUT, or DELETE). Headers are set via curl_setopt() and the response is returned by curl_exec().
main():
The entry point of the script. Sets the HTTP method, constructs the request path and signature, sends the request, and prints the response.
Notes
API Key & Secret:
Make sure to replace API_KEY and API_SECRET with the actual credentials from CatFee.IO.
Error Handling:
Errors from curl_exec() are caught with try-catch and displayed.
HTTP Methods:
The example supports POST, GET, PUT, and DELETE. Adjust based on your specific API endpoint.
Response Handling:
The response from curl_exec() is printed directly. You may want to parse it (e.g., using json_decode()) depending on the expected format.
Summary
This example demonstrates how to securely call the CatFee.IO Rest API using PHP. It uses HMAC-SHA256 for request signing, and cURL to send HTTP requests. The code supports multiple HTTP methods and is flexible for integration with different API endpoints.