One of the most popular Bitcoin websites, Blockchain.info, has a suite of Bitcoin APIs and in this tutorial, we’ll look at how two of those can be used to build a Bitcoin currency conversion application.
Getting started
To start, you will need a hosting account that supports PHP 5.0 or greater and has the PHP curl module installed. In the root web directory of your hosting account, create two empty PHP files, index.php and converter.php.The index page
Our index page, index.php, should contain a form that we can use to prompt the user for information about the currency conversion desired. In the empty index.php file, paste the following code:<html>
<head>
<title>Bitcoin Currency Converter</title>
</head>
<body>
<form action="converter.php" method="post">
<h1>Bitcoin Currency Converter</h1>
<p>
<label for="amount">Bitcoin Amount</label>
<input type="text" name="amount" id="amount">
</p>
<p>
<label for="currency">Currency</label>
<select name="currency" id="currency">
<option value="USD">USD</option>
<option value="EUR">EUR</option>
<option value="CNY">CNY</option>
<option value="JPY">JPY</option>
<option value="SGD">SGD</option>
<option value="HKD">HKD</option>
<option value="CAD">CAD</option>
<option value="AUD">AUD</option>
<option value="NZD">NZD</option>
<option value="GBP">GBP</option>
<option value="DKK">DKK</option>
<option value="SEK">SEK</option>
<option value="BRL">BRL</option>
<option value="CHF">CHF</option>
<option value="RUB">RUB</option>
<option value="SLL">SLL</option>
</select>
</p>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
</body>
</html>
Now our index page is in place:
The converter script
To perform the currency conversion, our index page will post data submitted through its form to converter.php. In the empty converter.php file, paste the following code:<html>
<head>
<title>Bitcoin Currency Converter</title>
</head>
<body>
<?php
// Make sure the user submitted all of the data required
if(isset($_POST['amount']) && is_numeric($_POST['amount']) && isset($_POST['currency'])) {
// Use curl to perform the currency conversion using Blockchain.info's currency conversion API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/tobtc?currency=" . $_POST['currency'] . "&value=" . $_POST['amount']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$conversion = curl_exec($ch);
// Use curl to get current prices and 15 minute averages for all currencies from Blockchain.info's exchange rates API
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/ticker");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$prices = json_decode(curl_exec($ch), true);
curl_close($ch);
?>
<h1>Conversion Results</h1>
<p><?php echo $_POST['amount']; ?> <?php echo $_POST['currency']; ?> is <?php echo $conversion; ?> BTC.</p>
<h2>Historical Prices</h2>
<p><strong>Last price:</strong> <?php echo $prices[$_POST['currency']]['last']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<p><strong>Buy price:</strong> <?php echo $prices[$_POST['currency']]['buy']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<p><strong>Sell price:</strong> <?php echo $prices[$_POST['currency']]['sell']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<p><strong>15 minute average price:</strong> <?php echo $prices[$_POST['currency']]['15m']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<?php } else { ?>
<p>Please fill out the form completely. <a href="index.php">Go back.</a></p>
<?php } ?>
</body>
</html>
The code above calls two endpoints associated with
Blockchain.info's Market Prices and Exchanges Rates API. The first
endpoint allows us perform the actual currency conversion for the
currency and amount supplied by the user. The second endpoint provides
real-time currency conversion information, such as the current buy and
sell prices and 15-minute average price. We can use this to display more
information to the user about the state of the market in their chosen
currency:
Adding a price chart
Blockchain.info offers a Statistics API that provides data that can be used to create charts. One of the endpoints provides historical data for the price of Bitcoin in U.S. dollars. To make Bitcoin-to-USD conversions more useful, we can add on to our converter.php script so it displays a chart of the BTC price over the past 60 days. To create the chart, we can use the Google Charts JavaScript API.Replace the existing converter.php code with the following:
<html>
<head>
<title>Bitcoin Currency Converter</title>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
</head>
<body>
<?php
// Make sure the user submitted all of the data required
if(isset($_POST['amount']) && is_numeric($_POST['amount']) && isset($_POST['currency'])) {
// Use curl to perform the currency conversion using Blockchain.info's currency conversion API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/tobtc?currency=" . $_POST['currency'] . "&value=" . $_POST['amount']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$conversion = curl_exec($ch);
// Use curl to get current prices and 15 minute averages for all currencies from Blockchain.info's exchange rates API
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/ticker");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$prices = json_decode(curl_exec($ch), true);
curl_close($ch);
?>
<h1>Conversion Results</h1>
<p><?php echo $_POST['amount']; ?> <?php echo $_POST['currency']; ?> is <?php echo $conversion; ?> BTC.</p>
<h2>Historical Prices</h2>
<p><strong>Last price:</strong> <?php echo $prices[$_POST['currency']]['last']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<p><strong>Buy price:</strong> <?php echo $prices[$_POST['currency']]['buy']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<p><strong>Sell price:</strong> <?php echo $prices[$_POST['currency']]['sell']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<p><strong>15 minute average price:</strong> <?php echo $prices[$_POST['currency']]['15m']; ?><?php echo $prices[$_POST['currency']]['symbol']; ?></p>
<?php
// Display the pricing chart if we're doing a US Dollar conversion
if($_POST['currency'] == "USD") {
// Use curl to get pricing chart data for the past 60 days
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://blockchain.info/charts/market-price?showDataPoints=true×pan=60days&show_header=true&daysAverageString=7&scale=0&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$chartdata = json_decode(curl_exec($ch), true);
?>
<div id="chart" style="width: 1000px; height: 500px;"></div>
<script type="text/javascript">
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Day', 'Price'],
<?php
// Loop through the x-y coordinates Blockchain.info's API provides and add them to a JavaScript array
foreach($chartdata["values"] as $xy) {
echo "['" . date("Y/m/d", $xy["x"]) . "'," . $xy["y"] . "],";
}
?>
]);
new google.visualization.LineChart(document.getElementById("chart")).draw(data, {curveType: "function",
width: 1000, height: 500,
vAxis: {maxValue: 800}}
);
}
drawChart();
</script>
<?php } ?>
<?php } else { ?>
<p>Please fill out the form completely. <a href="index.php">Go back.</a></p>
<?php } ?>
</body>
</html>
With our charting code in place, users performing
currency conversions in U.S. dollars can now visualize how the price has
changed over the past two months:




0 Comments