デベロッパードキュメント
PHP統合
PHPでiexbase/tron-apiを使用してTronEnergyエナジー委任を統合します。Composerインストール、すぐに使えるコード例を用意しています。
API支払いアドレス
TFqUiCu1JwLHHnBNeaaVKH7Csm4aA3YhZx
このアドレスにTRXを送金してください。あなたのトランザクションハッシュがエナジー委任の請求に使用されます。
完全なPHP統合は iexbase/tron-api ライブラリを使用しています。各ステップは独立したコードブロックで、プロジェクトにコピー&ペーストできます。あらゆるPHPフレームワークまたはプレーンPHPで動作します。
必要条件: PHP 7.4以上、Composer、ext-gmpおよびext-bcmathが有効、資金が十分なTronウォレット。
PHPでの署名について: TronEnergy APIはウォレット署名を使用して必要です
tronWeb.trx.signMessageV2()。iexbase/tron-api ライブラリはこの正確な署名プリミティブを公開していないため、このガイドでは署名ステップに小さなNode.jsヘルパーを使用しています。その他のすべて(TRX送金、API呼び出し、USDT送金)はPHPのままです。有効なsignMessageV2署名を生成するピュアPHP署名ソリューションがある場合は、他のステップを変更することなくヘルパーを置き換えることができます。
フロー
APIキーなし。サインアップなし。コードはオンチェーンでTronEnergyの支払いアドレスにTRXを送信し、所有権を証明するメッセージに署名してから委任を請求します。エナジーは約3秒で到着します。その後、コードは委任されたエナジーを使用してUSDTを送信します。
1
TRXを送信
支払いアドレスにオンチェーンで4 TRX以上を送信してください(最小4、最大1000)。
2
署名
tx_hash:delegate_toに署名して、送信者であることを証明してください。
3
請求
トランザクションハッシュと署名を使用してPOST /delegateを実行します。エナジーは約3秒で到着します。
料金は一律です:1 TRX あたり 16,250 エナジー。 最小注文額は 4 TRX(65,000 エナジー — 標準的な USDT 送金 1 回分)、最大は 1,000 TRX(16.25M エナジー)です。送金額により、委任されるエナジー量が正確に決まります — 階層やパッケージはありません。標準的な送金 1 回の場合は
$trxAmount = 4 をご使用ください。新しいウォレットからの送金の場合は 8 をご使用ください。バッチ処理の場合はさらに多く必要です。以下のコードでは $trxAmount 変数を使用しているため、1 箇所で変更できます。
ステップバイステップ
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 ヘルパーを使用します。PHP ファイルの隣に sign.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。その後 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";
エラーハンドリング
クレーム呼び出しを小さなリトライヘルパーでラップしてください。最も一般的なエラーは payment_verification_failed で、トランザクションがまだチェーン上にインデックスされていません — 数秒待機してから 1 回リトライしてください。
エラー処理
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";
}