PHP code example of amendozadev / eth_rpc
1. Go to this page and download the library: Download amendozadev/eth_rpc 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/ */
amendozadev / eth_rpc example snippets
$wallet = new \aMendoza\Eth_rpc\Wallet();
//Create new wallet
$account = $wallet->createWallet(); //['address' => '0xf1b4d0755ef13025c8b1b398237652b7c3ea8dc0', 'private_key' => '81011e578366208ab232d726cb910d1dffd132c84659cede2bfc6b5d0404d234']
//Connect to RPC URL (URL : Chain ID)
$wallet->rpc_url('https://ropsten.infura.io/v3/', 3);
// get gas price in wei
$wei = $wallet->gasPrice();
echo $wei; // "5000000000"
// convert to eth:
$eth = $wallet->wei2eth($wei);
echo $eth; // "0.000000005000000000"
// block number
$block = $wallet->blockNumber();
echo $block; //18143442
// account balance
$balance = $wallet->getBalance($account['address']);
//Send Transaction
try {
$to = '0xa48e2ff1e6e4ef5952c64dc505d6983a92d320f7';
$nonce = 0;
$gasPrice = 10;
$gasLimit = 21000;
$raw = $wallet->createTransaction($account['address'], $to, '0.0002', $account['private_key'], $nonce, $gasPrice, $gasLimit);
echo $wallet->sendTransaction($raw); //0xa91377938abd362e2486398d467a6c7c96b1100424ef1d20318e82bffbd8f0e8
}catch (Exception $exception){
echo $exception->getMessage();
}