April 25, 2019, 10:25:51 pm

Author Topic: Need GLD/USD Price Script for Gold Club backend  (Read 1362 times)

0 Members and 1 Guest are viewing this topic.

Offline MicroGuy

  • @realMicroGuy
  • Administrator
  • *****
  • Posts: 3445
  • Karma: +47/-0
  • "Shoot for the Stars!"
    • View Profile
    • MicroGuy.com
Need GLD/USD Price Script for Gold Club backend
« on: August 03, 2016, 06:18:04 pm »
Our friends at Gold Club need a PHP snippet that will provide the GLD/USD price in real time. In order to get an accurate price, I was thinking they should use the Coindesk Price Index API for the BTC/USD price and the C-CEX API for the GLD/BTC price.

I've managed to find those pieces of code, now we just need to tie the two together to calculate the GLD/USD price.

BTC/USD Code
Code: [Select]
<?php

// 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('fromemail@gmail.com''Downtime'"Notice: Coindesk seems to be down."'From: youremail@gmail.com');
$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('fromemail@gmail.com''Downtime'"Warning: blockchain.info seems to be down too!"'From: youremail@gmail.com');
$price_last 1000;
}
}

GLD/BTC Price

Code: [Select]
// 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('fromemail@gmail.com', 'Downtime', "Notice: C-CEX.com seems to be down.", 'From: youremail@microguy.net');
$price_last = .015;
}
"The heart of any cryptocurrency can be found in the spirit of its community."

Offline Stouse49

  • Gold Engineer
  • DevTeam
  • *****
  • Posts: 711
  • Karma: +66/-0
    • View Profile
Re: Need GLD/USD Price Script for Gold Club backend
« Reply #1 on: August 04, 2016, 09:48:55 am »
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:

Code: [Select]
<?php echo getGLDPrice() ?>

The code that calculates the price of GLD in USD.

Code: [Select]
<?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('fromemail@gmail.com''Downtime'"Notice: Coindesk seems to be down."'From: youremail@gmail.com');
$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('fromemail@gmail.com''Downtime'"Warning: blockchain.info seems to be down too!"'From: youremail@gmail.com');
$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('fromemail@gmail.com''Downtime'"Notice: C-CEX.com seems to be down."'From: youremail@microguy.net');
$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.
GLD: E1A6WogjpqU3KjjoRUD63f7kofrFozmyfg

Offline MicroGuy

  • @realMicroGuy
  • Administrator
  • *****
  • Posts: 3445
  • Karma: +47/-0
  • "Shoot for the Stars!"
    • View Profile
    • MicroGuy.com
Re: Need GLD/USD Price Script for Gold Club backend
« Reply #2 on: August 04, 2016, 10:28:10 am »
Thank you Eric so much for writing this code. I'll let Gilead know that it's here now.  :)
"The heart of any cryptocurrency can be found in the spirit of its community."

Offline tadspoles

  • Legendary
  • *****
  • Posts: 629
  • Karma: +46/-0
    • View Profile
Re: Need GLD/USD Price Script for Gold Club backend
« Reply #3 on: August 04, 2016, 05:14:06 pm »
I'd like to thank Stouse49 for all he does for GoldCoin. 

Offline goldclub

  • GoldCoin Africa
  • DreamTeam
  • *****
  • Posts: 12
  • Karma: +17/-0
    • View Profile
Re: Need GLD/USD Price Script for Gold Club backend
« Reply #4 on: August 05, 2016, 04:05:33 pm »
Thank you so much for this support , i have sent the codes to our program i will give you feedback on the out come of  the codes.

Thank you so much!!!