1. Go to this page and download the library: Download jackillll/tron library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Http\Controllers;
use Jackillll\Tron\Tron;
class TronController extends Controller
{
protected $tron;
public function __construct(Tron $tron)
{
$this->tron = $tron;
}
public function getBalance($address)
{
return $this->tron->getBalance($address, true);
}
public function sendTrx($to, $amount)
{
return $this->tron->send($to, $amount);
}
}
use Jackillll\Tron\Facades\Tron;
// Get account information
$account = Tron::getAccount('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t');
// Get balance
$balance = Tron::getBalance('TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', true);
// Send TRX
$result = Tron::send('TTo_Address_Here', 1.5);
// Get latest blocks
$blocks = Tron::getLatestBlocks(10);
namespace App\Services;
use Jackillll\Tron\Tron;
class TronService
{
protected $tron;
public function __construct(Tron $tron)
{
$this->tron = $tron;
}
public function getUserBalance($address)
{
try {
return $this->tron->getBalance($address, true);
} catch (\Exception $e) {
\Log::error('Tron balance error: ' . $e->getMessage());
return 0;
}
}
public function transferTrx($fromPrivateKey, $toAddress, $amount)
{
try {
$this->tron->setPrivateKey($fromPrivateKey);
return $this->tron->send($toAddress, $amount);
} catch (\Exception $e) {
\Log::error('Tron transfer error: ' . $e->getMessage());
return false;
}
}
}
ackillll\Tron\Tron;
use Jackillll\Tron\Provider\HttpProvider;
$fullNode = new HttpProvider('https://api.trongrid.io');
$solidityNode = new HttpProvider('https://api.trongrid.io');
$eventServer = new HttpProvider('https://api.trongrid.io');
try {
$tron = new Tron($fullNode, $solidityNode, $eventServer);
} catch (\Jackillll\Tron\Exception\TronException $e) {
exit($e->getMessage());
}
// Use the same way as above...
// Get latest blocks
$blocks = $tron->getLatestBlocks(10);
// Get block by number
$block = $tron->getBlock(12345);
// Get transaction by ID
$transaction = $tron->getTransaction('transaction_id_here');
// Get account transactions
$transactions = $tron->getTransactionsFromAddress('TAddress...', 50);
// Old way (still supported)
'host' => 'https://api.trongrid.io',
'node_type' => 'fullnode',
// New way (recommended)
'network' => 'mainnet',
'use_solidity' => false,