ที่อยู่ API สำหรับการชำระเงิน
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
ส่ง TRX ไปยังที่อยู่นี้ tx hash ของคุณจะใช้เพื่อขอรับการมอบหมายพลังงาน

บูรณาการ PHP แบบเต็มรูปแบบโดยใช้ไลบรารี iexbase/tron-api แต่ละขั้นตอนเป็นบล็อกโค้ดแยกที่สามารถคัดลอกไปยังโปรเจกต์ของคุณได้ ทำงานกับ PHP framework ใดๆ หรือ PHP ธรรมดา

ข้อกำหนดเบื้องต้น: PHP 7.4+ Composer ext-gmp และ ext-bcmath เปิดใช้งาน และกระเป๋าเงิน Tron ที่มีเงิน
เกี่ยวกับการเซ็นชื่อใน PHP: API ของ TronEnergy ต้องมีลายเซ็นกระเป๋าเงินโดยใช้ tronWeb.trx.signMessageV2() ไลบรารี iexbase/tron-api ไม่ได้เปิดเผยดั้ชดั้งเดิมการเซ็นชื่อนี้ ดังนั้นคู่มือนี้จึงใช้ helper Node.js ขนาดเล็กสำหรับขั้นตอนการเซ็นชื่อ ทุกอย่างอื่นๆ (การส่ง TRX การเรียก API การส่ง USDT) ยังคงใน PHP หากคุณมีโซลูชันการเซ็นชื่อ PHP แบบบริสุทธิ์ที่สร้างลายเซ็น signMessageV2 ที่ถูกต้อง คุณสามารถสลับ helper ออกโดยไม่เปลี่ยนขั้นตอนอื่นใด

ขั้นตอนการไหล

ไม่มี API key ไม่ต้องสมัครสมาชิก โค้ดของคุณส่ง TRX บน chain ไปยังที่อยู่ payment ของ TronEnergy เซ็นข้อความเพื่อพิสูจน์ความเป็นเจ้าของ จากนั้นขอรับการมอบหมายพลังงาน พลังงานจะมาถึงในประมาณ 3 วินาที จากนั้นโค้ดของคุณส่ง USDT โดยใช้พลังงานที่มอบหมาย

1
ส่ง TRX
ส่ง 4 TRX หรือมากกว่า ไปยังที่อยู่ payment บน chain (ขั้นต่ำ 4 สูงสุด 1000)
2
เซ็นชื่อ
เซ็น tx_hash:delegate_to เพื่อพิสูจน์ว่าคุณเป็นผู้ส่ง
3
ขอรับ
POST /delegate ด้วย tx hash และลายเซ็น พลังงานจะมาถึงในประมาณ 3 วินาที
ราคาเป็นเชิงเส้น: 16,250 Energy ต่อ 1 TRX ยอดสั่งซื้อขั้นต่ำ 4 TRX (65,000 Energy — โอน USDT มาตรฐานหนึ่งครั้ง) สูงสุด 1,000 TRX (16.25M Energy) จำนวนเงินที่คุณส่งจะกำหนดพลังงานที่ได้รับมอบหมายย้อนกลับมาเท่านั้น — ไม่มีระดับ ไม่มีแพ็คเกจ สำหรับโอน USDT มาตรฐานครั้งเดียว ใช้ $trxAmount = 4 สำหรับโอนกระเป๋าเงินใหม่ ใช้ 8 สำหรับการทำงานเป็นชุด ใช้มากขึ้น โค้ดด้านล่างใช้ตัวแปร $trxAmount เพื่อให้คุณสามารถเปลี่ยนแปลงได้ในที่เดียว

ทีละขั้นตอน

1. ติดตั้ง

composer
composer require iexbase/tron-api

2. ตั้งค่า

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
อย่าเขียนข้อมูลประจำตัวลงในโค้ด โหลดคีย์ส่วนตัวจากตัวแปรสภาพแวดล้อมหรือโปรแกรมจัดการความลับ อย่าคอมมิตความลับลงใน git

4. ส่ง TRX

ส่ง 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. ลงนามข้อความ

API ต้องใช้ลายเซ็นที่พิสูจน์ว่ากระเป๋าเงินเดียวกันที่ส่ง TRX กำลังขอการมอบหมายพลังงาน เราทำเช่นนี้ด้วยตัวช่วย Node.js เล็กน้อย บันทึกเป็น sign.js ถัดจากไฟล์ PHP ของคุณ:

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); });

ติดตั้ง TronWeb ในโฟลเดอร์เดียวกัน: npm install tronweb จากนั้นเรียกมันจาก PHP:

ลงนามจาก 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. รับการมอบหมายพลังงาน

เรียกร้องการมอบหมายพลังงาน
// $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. ส่ง USDT

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

การจัดการข้อผิดพลาด

ห่อการเรียก claim ในตัวช่วยลองใหม่เล็กน้อย ข้อผิดพลาดที่พบบ่อยที่สุดคือ payment_verification_failed เมื่อ tx ยังไม่ได้ทำดัชนีบนเชน — รอสักครู่แล้วลองใหม่อีกครั้ง

การจัดการข้อผิดพลาด
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'); }

ตัวอย่างที่สมบูรณ์

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