Tam PHP entegrasyonu iexbase/tron-api kütüphanesini kullanarak. Her adım, projenize kopyalayabileceğiniz bağımsız bir kod bloğudur. Herhangi bir PHP framework'ü veya saf PHP ile çalışır.
Ön koşullar: PHP 7.4+, Composer, ext-gmp ve ext-bcmath etkinleştirilmiş, fonlanan Tron cüzdanı.
PHP'de imzalama hakkında: TronEnergy API'si tronWeb.trx.signMessageV2() kullanarak cüzdan imzası gerektirir. iexbase/tron-api kütüphanesi bu tam imzalama ilkel tipini sunmadığından, bu rehber imzalama adımı için küçük bir Node.js yardımcısını kullanır. Diğer her şey (TRX gönderme, API çağrısı, USDT gönderme) PHP'de kalır. Geçerli bir signMessageV2 imzası üreten saf-PHP imzalama çözümünüz varsa, yardımcıyı başka bir adımı değiştirmeden değiştirebilirsiniz.
Akış
API anahtarı yok. Kayıt yok. Kodunuz zincir üzerinde TronEnergy ödeme adresine TRX gönderir, sahipliğini kanıtlayan bir mesajı imzalar, ardından delegasyonu talep eder. Enerji yaklaşık 3 saniyede gelir. Ardından kodunuz delege edilen enerjiyi kullanarak USDT gönderir.
1
TRX Gönderin
Zincir üzerinde ödeme adresine 4 veya daha fazla TRX gönderin (min 4, maks 1000).
2
İmzalayın
Gönderici olduğunuzu kanıtlamak için tx_hash:delegate_to imzalayın.
3
Talep Edin
İşlem hash'i ve imzası ile /delegate'e POST yapın. Enerji ~3s içinde gelir.
Fiyatlandırma doğrusaldır: TRX başına 16.250 Enerji. Minimum sipariş 4 TRX (65.000 Enerji — bir standart USDT transferi), maksimum 1.000 TRX (16,25M Enerji). Gönderdiğiniz miktar, tam olarak ne kadar Enerji delege edileceğini belirler — katmanlar yok, paketler yok. Tek bir standart transfer için, $trxAmount = 4 kullanın. Yeni cüzdan transferi için, 8 kullanın. Toplu işler için daha fazlasını kullanın. Aşağıdaki kod bir $trxAmount değişkeni kullanır, böylece tek bir yerde değiştirebilirsiniz.
Adım Adım
1. Yükleyin
composer require iexbase/tron-api
2. Kurun
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
Kimlik bilgilerini asla kodlamayın. Özel anahtarınızı ortam değişkenlerinden veya bir sırlar yöneticisinden yükleyin. Sırları asla git'e işlemeyin.
4. TRX Gönderin
// 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. Mesajı İmzalayın
API, TRX gönderen aynı cüzdanın delegasyon istediğini kanıtlayan bir imza gerektirir. Bunu küçük bir Node.js yardımcısıyla yaparız. Bunu PHP dosyanızın yanında sign.js olarak kaydedin:
// 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'i aynı klasöre yükleyin: npm install tronweb. Ardından PHP'den çağırın:
// 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. Delegasyonu İste
// $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 Gönderin
// Energy is now delegated. Send USDT.
$contract = $tron->contract($usdt);
$transfer = $contract->transfer($delegateTo, 10 * pow(10, 6));
echo "USDT sent: " . $transfer . "\n";
Hata İşleme
İstek çağrısını küçük bir yeniden deneme yardımcısıyla sarmalayın. En yaygın hata, payment_verification_failed işlem henüz zincirbilgisinde indekslenmediğinde oluşur — birkaç saniye bekleyin ve bir kez daha deneyin.
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');
}
Tam Örnek
<?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";
}