This code may work (as there might a a bug or two), but it has the general idea:
This PHP code can go in its own file and then be included in the HTML file that the information is needed.
To display the price then simply put this line in the HTML file in the place that the price should be displayed:
<?php echo getGLDPrice() ?>The code that calculates the price of GLD in USD.
<?php
function getGLDPrice()
{
// request data from coindesk.com
$request_coindesk = json_decode(file_get_contents('http://api.coindesk.com/v1/bpi/currentprice/USD.json'), TRUE);
// check if coindesk.com is up
if ( isset($request_coindesk) )
{
// get the last price
$price_last = $request_coindesk['bpi']['USD']['rate'];
}
else
{
// if coindesk.com is down, send an email and request data from blockchain.info
mail('[email protected]', 'Downtime', "Notice: Coindesk seems to be down.", 'From: [email protected]');
$request_blockchain_info = json_decode(file_get_contents('http://blockchain.info/ticker'), TRUE);
// check if blockchain.info is up
if ( isset($request_blockchain_info) )
{
// get the last price
$price_last = $request_blockchain_info['USD']['last'];
}
else
{
// if coindesk and blockchain.info are down, send an email and set the last price to 1000 temporarily.
mail('[email protected]', 'Downtime', "Warning: blockchain.info seems to be down too!", 'From: [email protected]');
$price_last = 1000;
}
return getGLDPriceInBTC() * $price_last;
}
function getGLDPriceInBTC()
{
// request data from c-cex.com
$request_ccex = json_decode(file_get_contents('https://c-cex.com/t/gld-btc.json'), TRUE);
// check if c-cex.com is up
if ( isset($request_ccex) )
{
// get the last price
$price_last = $request_ccex['ticker']['avg'];
}
else
{
// if c-cex is down, send an email and set price to .015 temporarily
mail('[email protected]', 'Downtime', "Notice: C-CEX.com seems to be down.", 'From: [email protected]');
$price_last = .015;
}
return $price_last;
}
?>To make this code more robust it should have a backup exchange to get the Bitcoin Price and a Backup exchange to get the GoldCoin price. It should format the results based on the currency. It should also allow any currency to be chosen.