Bitcoin Forum

Economy => Service Discussion => Topic started by: thebenjamincode on April 27, 2015, 12:32:25 PM



Title: Show balance of BTC using PHP question
Post by: thebenjamincode on April 27, 2015, 12:32:25 PM
hi there everyone, i am new to programming using api
does anyone here can share a code about showing the balance of a btc address using php?

please give me the code because i can't understand the api
thank you! :D


Title: Re: Show balance of BTC using PHP question
Post by: coinableS on April 27, 2015, 12:49:06 PM
You can do this easily with blockchain.info's API.

Checkout their documentation and scroll down to "Single Address": https://blockchain.info/api/blockchain_api (https://blockchain.info/api/blockchain_api)
 
https://blockchain.info/address/$bitcoin_address?format=json

Using this you can get the balance in JSON format and parse it with PHP.

Code:
//Declare an address as a variable
$btc_address = "1J9ikqFuwrzPbczsDkquA9uVYeq6dEehsj";

//Get the balance using blockchaininfo API
$addr_url = "https://blockchain.info/address/$btc_address?format=json";
$json_addr = json_decode(file_get_contents($addr_url), true);
$balance = $json_addr["final_balance"];

//Display the balance on the screen in satoshis
echo $balance;

//Display on the screen as bitcoins
$con_bal = $balance / 100000000;
echo "<br>". $con_bal;



Title: Re: Show balance of BTC using PHP question
Post by: thebenjamincode on April 27, 2015, 03:12:44 PM
You can do this easily with blockchain.info's API.

Checkout their documentation and scroll down to "Single Address": https://blockchain.info/api/blockchain_api (https://blockchain.info/api/blockchain_api)
 
https://blockchain.info/address/$bitcoin_address?format=json

Using this you can get the balance in JSON format and parse it with PHP.

Code:
//Declare an address as a variable
$btc_address = "1J9ikqFuwrzPbczsDkquA9uVYeq6dEehsj";

//Get the balance using blockchaininfo API
$addr_url = "https://blockchain.info/address/$btc_address?format=json";
$json_addr = json_decode(file_get_contents($addr_url), true);
$balance = $json_addr["final_balance"];

//Display the balance on the screen in satoshis
echo $balance;

//Display on the screen as bitcoins
$con_bal = $balance / 100000000;
echo "<br>". $con_bal;



thank you so much for helping me :D