का उपयोग करके पूर्ण PHP एकीकरण iexbase/tron-api library। प्रत्येक चरण एक स्वतंत्र कोड ब्लॉक है जिसे आप अपने प्रोजेक्ट में कॉपी कर सकते हैं। किसी भी PHP framework या सादे PHP के साथ काम करता है।
पूर्वापेक्षाएँ: PHP 7.4+, Composer, ext-gmp और ext-bcmath सक्षम, एक funded Tron wallet।
PHP में signing के बारे में: TronEnergy API को wallet signature की आवश्यकता है tronWeb.trx.signMessageV2()। iexbase/tron-api library इस सटीक signing primitive को expose नहीं करता है, इसलिए यह गाइड signing चरण के लिए एक छोटे Node.js helper का उपयोग करता है। बाकी सब कुछ (TRX भेजना, API को कॉल करना, USDT भेजना) PHP में रहता है। यदि आपके पास एक शुद्ध-PHP signing समाधान है जो एक वैध signMessageV2 signature बनाता है, तो आप किसी अन्य चरण को बदले बिना helper को स्वैप कर सकते हैं।
प्रवाह
कोई API key नहीं। कोई साइन-अप नहीं। आपका कोड TronEnergy भुगतान पते पर on-chain TRX भेजता है, स्वामित्व साबित करने के लिए एक संदेश पर हस्ताक्षर करता है, फिर delegation दावा करता है। Energy लगभग 3 सेकंड में आ जाती है। फिर आपका कोड delegated energy का उपयोग करके USDT भेजता है।
1
TRX भेजें
भुगतान पते पर on-chain 4 या अधिक TRX भेजें (न्यूनतम 4, अधिकतम 1000)।
2
Sign करें
tx_hash:delegate_to पर हस्ताक्षर करें यह साबित करने के लिए कि आप भेजने वाले हैं।
3
दावा करें
tx hash और signature के साथ POST /delegate करें। Energy ~3s में आ जाती है।
मूल्य निर्धारण linear है: 1 TRX पर 16,250 Energy।न्यूनतम ऑर्डर 4 TRX (65,000 Energy — एक standard USDT transfer), अधिकतम 1,000 TRX (16.25M Energy)। आप जो राशि भेजते हैं वह ठीक उतना ही Energy वापस delegate करती है — कोई tiers नहीं, कोई packages नहीं। एक single standard transfer के लिए, $trxAmount = 4 का उपयोग करें। एक नए wallet transfer के लिए, 8 का उपयोग करें। batch काम के लिए, अधिक का उपयोग करें। नीचे दिया गया कोड एक $trxAmount variable का उपयोग करता है ताकि आप इसे एक जगह बदल सकें।
Step by Step
1. Install करें
composer require iexbase/tron-api
2. Setup करें
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
कभी भी credentials को hardcode न करें। अपनी private key को environment variables या secrets manager से लोड करें। कभी भी secrets को git में commit न करें।
4. 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. Message को Sign करें
API को एक signature की जरूरत है जो साबित करे कि जिस wallet ने TRX भेजा वही delegation के लिए ask कर रहा है। हम इसे एक छोटे Node.js helper से करते हैं। इसे अपनी PHP file के बगल में sign.js के रूप में save करें:
// 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); });
इसी folder में TronWeb install करें: npm install tronweb। फिर इसे PHP से call करें:
// 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. Delegation को Claim करें
// $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 भेजें
// Energy is now delegated. Send USDT.
$contract = $tron->contract($usdt);
$transfer = $contract->transfer($delegateTo, 10 * pow(10, 6));
echo "USDT sent: " . $transfer . "\n";
Error Handling
claim call को एक छोटे retry helper में wrap करें। सबसे common error है payment_verification_failed जब tx अभी on-chain पर indexed नहीं है — कुछ सेकंड रुकें और एक बार फिर से try करें।
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');
}
Complete Example
<?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";
}