-1

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;

1 Answers1

1

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/

Community
  • 1
  • 1
Kevin Labécot
  • 2,005
  • 13
  • 25
  • can you put it in a jsfiddle for me, i get no alert. When i paste this url below in the browser i get a balance in the form of a number (0). I just want to retrieve that number using js. I tried turning it into a variable but no joy.. **https://blockchain.info/de/q/addressbalance/17jJBRxVdNX4uP2gjs5t9LEMJ56zAXK7iC** – Fazeum Lifern Jul 14 '14 at 09:05