کا استعمال کرتے ہوئے مکمل پی ایچ پی انضمام iexbase/tron-api لائبریری ہر قدم ایک اسٹینڈ کوڈ بلاک ہے جسے آپ اپنے پروجیکٹ میں کاپی کر سکتے ہیں۔ کسی بھی پی ایچ پی فریم ورک یا سادہ پی ایچ پی کے ساتھ کام کرتا ہے۔
شرائط: PHP 7.4+، کمپوزر، ext-gmp اور ext-bcmath فعال، ایک فنڈڈ Tron والیٹ۔
پی ایچ پی میں سائن ان کرنے کے بارے میں: TronEnergy API استعمال کرتے ہوئے والیٹ کے دستخط کی ضرورت ہے۔ tronWeb.trx.signMessageV2(). دی iexbase/tron-api لائبریری اس قطعی دستخطی پرائمٹیو کو بے نقاب نہیں کرتی ہے، اس لیے یہ گائیڈ دستخطی مرحلے کے لیے ایک چھوٹے Node.js مددگار کا استعمال کرتا ہے۔ باقی سب کچھ ( TRX بھیجنا، API کو کال کرنا، USDT بھیجنا) PHP میں رہتا ہے۔ اگر آپ کے پاس ایک خالص-PHP دستخط کرنے کا حل ہے جو ایک درست signMessageV2 دستخط تیار کرتا ہے، تو آپ کسی دوسرے قدم کو تبدیل کیے بغیر ہیلپر کو تبدیل کر سکتے ہیں۔
بہاؤ
کوئی API کلید نہیں۔ کوئی سائن اپ نہیں۔ آپ کا کوڈ TronEnergy ادائیگی کے پتے پر TRX آن چین بھیجتا ہے، ملکیت ثابت کرنے والے پیغام پر دستخط کرتا ہے، پھر وفد کا دعویٰ کرتا ہے۔ Energy تقریباً 3 سیکنڈ میں پہنچ جاتی ہے۔ پھر آپ کا کوڈ تفویض کردہ توانائی کا استعمال کرتے ہوئے USDT بھیجتا ہے۔
1
TRX بھیجیں۔
4 یا اس سے زیادہ TRX آن چین ادائیگی کے پتے پر بھیجیں (کم سے کم 4، زیادہ سے زیادہ 1000)۔
2
دستخط
یہ ثابت کرنے کے لیے tx_hash:delegate_to پر دستخط کریں کہ آپ بھیجنے والے ہیں۔
3
دعویٰ
tx ہیش اور دستخط کے ساتھ POST/ مندوب۔ Energy ~3 سیکنڈ میں پہنچتی ہے۔
قیمت کا تعین لکیری ہے: 16,250 Energy فی TRX ۔ Minimum order 4 TRX (65,000 Energy — one standard USDT transfer), maximum 1,000 TRX (16.25M Energy). The amount you send determines exactly how much Energy is delegated back — no tiers, no packages. For a single standard transfer, use $trxAmount = 4. For a new-wallet transfer, use 8. For batch work, use more. The code below uses a $trxAmount variable so you can change it in one place.
قدم بہ قدم
1. انسٹال کریں۔
composer require iexbase/tron-api
2. سیٹ اپ
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
کبھی بھی ہارڈ کوڈ اسناد نہ بنائیں۔ Use environment variables or a secrets manager. The example above is for illustration only.
3. ادائیگی کی معلومات حاصل کریں (اختیاری)
// Get pricing and payment address (optional, energy is always available)
$supply = json_decode(
file_get_contents("${api}/supply"),
true
);
echo "Pay to: " . $supply['pay_to'] . "\n";
echo "Energy per TRX: " . $supply['energy_per_trx'] . "\n";
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. پیغام پر دستخط کریں۔
API کو ایک دستخط کی ضرورت ہوتی ہے جو ثابت کرتا ہے کہ وہی پرس جس نے TRX بھیجا تھا وفد کے لیے پوچھ رہا ہے۔ ہم یہ ایک چھوٹے سے Node.js مددگار کے ساتھ کرتے ہیں۔ اسے بطور محفوظ کریں۔ 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. پھر اسے پی ایچ پی سے کال کریں:
// 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,
]),
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$result = json_decode($response, true);
if (isset($result['error'])) {
throw new Exception("Delegation failed: " . $result['message']);
}
echo "Delegated: " . $result['energy'] . " energy\n";
echo "Ref: " . $result['ref'] . "\n";
7. USDT بھیجیں۔
// Send USDT using the delegated energy
$usdtContract = $tron->contract($usdt);
// Amount in smallest unit (6 decimals for USDT)
// 10 USDT = 10 * 1,000,000 = 10000000
$amount = 10 * pow(10, 6);
$recipient = 'TRecipientAddress';
$transfer = $usdtContract->transfer($recipient, $amount);
echo "USDT sent: " . $transfer . "\n";
ایرر ہینڈلنگ
کلیم کال کو ایک چھوٹے سے دوبارہ کوشش کرنے والے مددگار میں لپیٹیں۔ سب سے عام غلطی ہے۔ 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');
}
مکمل مثال
<?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";
}