ĐỊA CHỈ THANH TOÁN API
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
Gửi TRX đến địa chỉ này. Hash tx của bạn được dùng để yêu cầu ủy quyền năng lượng.

Tích hợp PHP đầy đủ sử dụng iexbase/tron-api thư viện. Mỗi bước là một khối mã độc lập mà bạn có thể sao chép vào dự án của mình. Hoạt động với bất kỳ framework PHP nào hoặc PHP thuần.

Yêu cầu tiên quyết: PHP 7.4+, Composer, ext-gmp và ext-bcmath được bật, một ví Tron được tài trợ.
Về ký trong PHP: API TronEnergy yêu cầu chữ ký ví sử dụng tronWeb.trx.signMessageV2(). Thư viện iexbase/tron-api không công khai kiểu ký này, vì vậy hướng dẫn này sử dụng một trợ giúp Node.js nhỏ cho bước ký. Mọi thứ khác (gửi TRX, gọi API, gửi USDT) ở lại trong PHP. Nếu bạn có giải pháp ký thuần PHP tạo chữ ký signMessageV2 hợp lệ, bạn có thể thay thế trợ giúp mà không thay đổi bước nào khác.

Quy Trình

Không có khóa API. Không đăng ký. Mã của bạn gửi TRX trên chuỗi đến địa chỉ thanh toán TronEnergy, ký tin nhắn chứng minh quyền sở hữu, rồi yêu cầu ủy quyền. Năng lượng đến trong khoảng 3 giây. Sau đó mã của bạn gửi USDT sử dụng năng lượng được ủy quyền.

1
Gửi TRX
Gửi 4 hoặc nhiều TRX đến địa chỉ thanh toán trên chuỗi (tối thiểu 4, tối đa 1000).
2
Ký tx_hash:delegate_to để chứng minh bạn là người gửi.
3
Yêu cầu
POST /delegate với hash tx và chữ ký. Năng lượng đến trong ~3s.
Giá cả là tuyến tính: 16,250 Energy trên mỗi TRX. Đơn hàng tối thiểu 4 TRX (65,000 Energy — một lần chuyển USDT tiêu chuẩn), tối đa 1,000 TRX (16.25M Energy). Số tiền bạn gửi xác định chính xác lượng Energy được ủy quyền lại — không có tầng, không có gói. Để chuyển tiêu chuẩn đơn lẻ, hãy dùng $trxAmount = 4. Để chuyển ví mới, hãy dùng 8. Để làm việc hàng loạt, dùng nhiều hơn. Code dưới đây dùng một $trxAmount biến nên bạn có thể thay đổi nó ở một chỗ.

Từng Bước

1. Cài đặt

composer
composer require iexbase/tron-api

2. Thiết lập

setup.php
require_once 'vendor/autoload.php'; use IEXBase\TronAPI\Tron; $tron = new Tron(); $tron->setPrivateKey('YOUR_PRIVATE_KEY'); $tron->setAddress('YOUR_WALLET_ADDRESS'); $api = 'https://api.tronnrg.com'; $addr = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx'; // API payment address $usdt = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'; // USDT contract
Không bao giờ hardcode thông tin đăng nhập. Tải khóa riêng tư từ các biến môi trường hoặc trình quản lý bí mật. Không bao giờ commit bí mật lên git.

4. Gửi TRX

gửi TRX
// Send TRX to the API payment address — pricing is linear. // 16,250 energy per TRX. Min 4 TRX, max 1,000 TRX. // $trxAmount = 4 → 65,000 energy (standard USDT transfer) // $trxAmount = 8 → 130,000 energy (new wallet transfer) // $trxAmount = 40 → 650,000 energy (10 standard transfers) // $trxAmount = 1000 → 16,250,000 energy (max) $trxAmount = 4; $payment = $tron->sendTrx($addr, $trxAmount); if (!isset($payment['result']) || !$payment['result']) { throw new Exception('TRX transfer failed'); } $txHash = $payment['txid']; echo "Payment sent: ${txHash}\n";

5. Ký Thông Điệp

API yêu cầu một chữ ký chứng minh rằng cùng một ví đã gửi TRX đang yêu cầu ủy quyền. Chúng tôi làm điều này với một trợ giúp Node.js nhỏ. Lưu nó dưới tên sign.js cạnh tệp PHP của bạn:

sign.js
// Usage: node sign.js <tx_hash> <delegate_to> // Outputs the signature to stdout. Reads private key from TRON_PRIVATE_KEY env var. const { TronWeb } = require('tronweb'); const [, , txHash, delegateTo] = process.argv; const tronWeb = new TronWeb({ fullHost: 'https://api.trongrid.io', privateKey: process.env.TRON_PRIVATE_KEY, }); tronWeb.trx.signMessageV2(`${txHash}:${delegateTo}`) .then(sig => process.stdout.write(sig)) .catch(e => { console.error(e.message); process.exit(1); });

Cài đặt TronWeb trong cùng thư mục: npm install tronweb. Sau đó gọi nó từ PHP:

ký từ PHP
// Both the sender (in $tron) and the signer must be the SAME wallet. // Make sure TRON_PRIVATE_KEY in your environment matches the wallet that sent the TRX. $delegateTo = 'TWalletThatNeedsEnergy'; $signature = trim(shell_exec( sprintf('node sign.js %s %s', escapeshellarg($txHash), escapeshellarg($delegateTo) ) )); if (!$signature) { throw new Exception('Signing failed. Check that node and tronweb are installed and TRON_PRIVATE_KEY is set.'); } echo "Signed: " . substr($signature, 0, 20) . "...\n";

6. Nhận Ủy Quyền

yêu cầu ủy quyền năng lượng
// $txHash, $delegateTo, and $signature are all defined in the previous steps. $ch = curl_init("${api}/delegate"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'tx_hash' => $txHash, 'delegate_to' => $delegateTo, 'signature' => $signature, ]), ]); $result = json_decode(curl_exec($ch), true); curl_close($ch); echo "Energy: " . $result['energy'] . "\n"; echo "Ref: " . $result['ref'] . "\n";

7. Gửi USDT

gửi USDT
// Energy is now delegated. Send USDT. $contract = $tron->contract($usdt); $transfer = $contract->transfer($delegateTo, 10 * pow(10, 6)); echo "USDT sent: " . $transfer . "\n";

Xử Lý Lỗi

Bọc lệnh gọi claim trong một trợ giúp thử lại nhỏ. Lỗi phổ biến nhất là payment_verification_failed khi tx chưa được lập chỉ mục trên chuỗi — chờ vài giây và thử lại một lần.

xử lý lỗi
function claimDelegation($api, $txHash, $delegateTo, $signature, $retries = 3) { for ($i = 0; $i < $retries; $i++) { $ch = curl_init("${api}/delegate"); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_POSTFIELDS => json_encode([ 'tx_hash' => $txHash, 'delegate_to' => $delegateTo, 'signature' => $signature, ]), ]); $result = json_decode(curl_exec($ch), true); curl_close($ch); if (!isset($result['error'])) { return $result; // Success } switch ($result['error']) { case 'payment_verification_failed': // Most common: tx not yet indexed. Wait and retry. sleep(3); continue 2; case 'hash_already_used': throw new Exception('This tx hash has already been claimed'); case 'signature_mismatch': throw new Exception('Signer does not match payment sender. Sign with the same wallet that sent TRX.'); case 'delegation_failed': // Refund queued automatically if payment was verified throw new Exception('Delegation failed: ' . $result['message']); default: throw new Exception($result['message'] ?? 'Unknown error'); } } throw new Exception('Transaction not found after retries'); }

Ví Dụ Hoàn Chỉnh

delegate-energy.php
<?php require_once 'vendor/autoload.php'; use IEXBase\TronAPI\Tron; $tron = new Tron(); $tron->setPrivateKey(getenv('TRON_PRIVATE_KEY')); $tron->setAddress(getenv('TRON_WALLET_ADDRESS')); $api = 'https://api.tronnrg.com'; $addr = 'TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx'; try { $delegateTo = 'TRecipientWallet'; $trxAmount = 4; // min 4, max 1000 — energy = trxAmount × 16,250 // 1. Send TRX (linear pricing: 16,250 energy per TRX) $payment = $tron->sendTrx($addr, $trxAmount); $txHash = $payment['txid']; echo "Payment: ${txHash}\n"; // 2. Sign via Node helper (see Step 5 above) $signature = trim(shell_exec( sprintf('node sign.js %s %s', escapeshellarg($txHash), escapeshellarg($delegateTo) ) )); if (!$signature) throw new Exception('Signing failed'); // 3. Claim delegation (with retry) $result = claimDelegation($api, $txHash, $delegateTo, $signature); echo "Energy: " . $result['energy'] . "\n"; echo "Ref: " . $result['ref'] . "\n"; // 4. Send USDT (energy is now available) $contract = $tron->contract('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'); $transfer = $contract->transfer($delegateTo, 10 * pow(10, 6)); echo "USDT sent: ${transfer}\n"; } catch (Exception $e) { echo "Error: " . $e->getMessage() . "\n"; }
Telegram WhatsApp