USD to BTC conversion code

2

1

So, I have looked everywhere and no one seems to have this specific problem.

I have set up a e-commerce store that is accepting BTC, So far the users can only see the price of the product in USD, however, I want the user to be able to click the USD price and convert it to BTC instantly. Like you can on a blockchain.info transaction.

For example:

Blockchain transaction conversion

So far, I have only been able to display the BTC price using Blockchain API.

I want the same effect as Blockchain on my price, or atleast under it.

(Snapshots of site below to give an idea of what I want)

My images

The code I have to display the BTC price (If it helps, im a noob sorry)

<?php

  $url = "https://blockchain.info/stats?format=json";
  $stats = json_decode(file_get_contents($url), true);

  echo $stats['market_price_usd'];

  ?>

Price display:

<p class="Price">$200.50</p>

Thomas Farley

Posted 2016-08-30T21:07:36.460

Reputation: 23

It took me a bit to realize what your question is. Could you please make that clearer?Murch 2016-08-30T21:20:11.423

See here https://github.com/miohtama/bitcoin-prices

Mikko Ohtamaa 2016-08-30T22:16:33.117

@SvenWilliamson Most of these seem irrelevant to the question.Nick ODell 2016-08-30T23:17:55.757

Answers

0

Your question is a bit confusing but it sounds like you are asking how to get a currency amount worth of bitcoin.

I want the user to be able to click the USD price and convert it to BTC instantly.

The answer is simple, division.

<?php
$url = "https://blockchain.info/stats?format=json";
$stats = json_decode(file_get_contents($url), true);
$btcValue = $stats['market_price_usd'];
$usdCost = 200;

$convertedCost = $usdCost / $btcValue;

echo $convertedCost;
?>     

http://phpfiddle.org/lite/code/cxsk-b62m

(If it helps, im a noob sorry)

That's okay. If you're looking to learn more beginner related Bitcoin coding stuff check out this mega-thread at bitcointalk https://bitcointalk.org/index.php?topic=990348.0 It's mostly PHP and JavaScript examples.

m1xolyd1an

Posted 2016-08-30T21:07:36.460

Reputation: 3 356

I'll test it now, thank you very much man !!Thomas Farley 2016-08-31T17:32:28.810

Yep, I suppose I can work with this, thanks buddy.Thomas Farley 2016-08-31T17:41:58.310