Search code examples
phpjqueryjsonbitcoin

bitcoin address function convert from php to jquery?


Can someone convert this function from php to jquery? Im not sure a file_get_contents equivalent exists.

(this php function just grabs the balance of a given bitcoin address, in this case the balances are zero for all four so it should return 0 0 0 0)

<?php
function getBalance($adress) {
    return file_get_contents('https://blockchain.info/de/q/addressbalance/'. $adress);
}

$addy1_balance = getBalance('19F16Tg47PUsie2vNzkjJn5xsgjAPU3z2f') / 100000000;
$addy2_balance = getBalance('17jJBRxVdNX4uP2gjs5t9LEMJ56zAXK7iC') / 100000000;
$addy3_balance = getBalance('13FBnfCFvJmketncTwr2csXJrzwU4wPLqe') / 100000000; 
$addy4_balance = getBalance('17tGnUhPNTF1L2NpfmmKWLFQS5teRGJSaf') / 100000000; 

?>


echo $addy1_balance;
echo $addy2_balance;
echo $addy3_balance;
echo $addy4_balance;

Solution

  • Use AJAX. It will be easier to use a library like jQuery. With jQuery you can use jQuery.getJSON. It only works if blockchain.info allow cross domain call.

    jQuery.getJSON('https://blockchain.info/de/q/addressbalance/17jJBRxVdNX4uP2gjs5t9LEMJ56zAXK7iC', function(result) {
    alert(result);
    });
    

    In pure javascript, see How to write JSONP Ajax request on pure js?.

    edit : Jsfiddle : http://jsfiddle.net/smM2T/