I want to incorporate a javascript in php that list all the accounts and shows balances for those. This is my script for that:

<script language="javascript">
function checkAllBalances() {

var totalBal = 0;

for (var acctNum in eth.accounts) {

    var acct = eth.accounts[acctNum];

    var acctBal = web3.fromWei(eth.getBalance(acct), "ether");

    totalBal += parseFloat(acctBal);

    console.log("  eth.accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether");

    alert("eth.accounts[" + acctNum + "]: ");


console.log("  Total balance: " + totalBal + " ether");

I incorporated this into php and and call it but it's not executing it properly. Any ideas?

  • If I wasn't clear, I wanted this script to run in geth console. My educated guess would be that javascript doesn't recognize eth. when it is being run inside php without some kind of library being imported, but I don't know how to do that either – kjrew2001 Feb 6 '18 at 7:50
  • I'm confused what this has to do with PHP. Can you elaborate on that? – Nanolucas Feb 6 '18 at 8:41
  • I am attempting to write a php code that uses this inside so I can use it sort of like an api. – kjrew2001 Feb 6 '18 at 10:32
  • How on earth do you expect the javascript that comes via PHP to your Browser to run inside a geth console? – rustyx Jul 2 '18 at 13:30

It's actually pretty simple done in PHP using digitaldonkey/ethereum-php library

$eth = new Ethereum('');
$balanceInWei = $eth->eth_getBalance(new EthD20($address), new EthBlockParam())->val();
$inEther = $eth->convertCurrency($balanceInWei);

See: https://github.com/digitaldonkey/ethereum-php/blob/master/public/list-balance-example.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.