CatFee.IO
Buy Tron EnergyDashboard
English
English
  • Introduction
  • Getting started
    • Quick Start Guide
    • Buy Energy via Direct TRX Transfer
    • Buy Energy via DApp
    • Buy Energy via API
      • API Overview
      • Java Example for Calling API
      • Python Example for Calling API
      • PHP Example for Calling API
      • Node.js Example for Calling API
      • Go Example for Calling API
    • API Supports Idempotency
    • Energy Subleasing Service
    • 💎Telegram Premium Reselling
    • Blockchain Monitoring Service
    • TRON Node Connection Guide
    • Frequently Asked Questions (FAQ)
    • Terms of Service
  • API Reference
    • Account Information
    • Buy Energy
    • Get Order Detail
    • Estimated Order Amount
    • Buy Telegram Premium
  • knowledge base
    • What is TRON Energy?
    • Why Rent Energy on the TRON Network?
    • How to fix “OUT OF ENERGY” error on TRON Transfers?
    • Why Can’t You Use a Centralized Exchange (CEX) Wallet to Buy TRON Energy?
    • What is a Centralized Exchange (CEX)?
    • How Much Energy and Bandwidth Does a USDT Transfer Consume?
    • Why Does the Transfer Fail Even After Renting Energy on TRON?
    • Why Are Some Energy Rentals So Cheap?
  • Telegram Bot
    • How to Create a Telegram Bot and Host It on CatFee?
Powered by GitBook
On this page

Was this helpful?

  1. Getting started
  2. Buy Energy via API

PHP Example for Calling API

PHP Example for Calling the CatFee.IO REST API

Prerequisites

  • You need a valid API Key and API Secret.

  • 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.

PreviousPython Example for Calling APINextNode.js Example for Calling API

Last updated 19 days ago

Was this helpful?