Bitcoin to US Dollar conversion in PHP

In this example we will use cryptonator's api to show a conversion from bitcoin to USD.

For more information about the api visit – https://www.cryptonator.com/api/. From the website

Cryptonator provides unique volume of cryptocurrency exchange rates data, which is delivered in easy-to-integrate JSON format via simple HTTPS requests. Prices are updated every 30 seconds, covering 300+ cryptocurrencies across 62 exchanges.

You can get a list of cryptocurrencies supported by visiting http://www.cryptonator.com/api/currencies

Lets take a look at an example in PHP

Code

[php]

<?php
$url = “https://www.cryptonator.com/api/ticker/btc-usd”;
$data = json_decode(file_get_contents($url), true);
$ticker = $data[‘ticker'];
$latest_price = $ticker[‘price'];
echo “1 Bitcoin = ” . “$latest_price” . ” US Dollars”;
?>

<html>
<head>
<title>BTC to USd Price Converter</title>
<meta content='10' http-equiv='refresh'/> <!– Reloads Page every 10 seconds –>
</head>

<body>
</body>

</html>

[/php]

 

Output

You should see something like this, depending on the current bitcoin price

1 Bitcoin = 9127.96826769 US Dollars

Related posts

Displaying the bitcoin price using the coindesk API

Let’s build a blockchain in Ruby

Cryptocurrency Blockchain in 30 minutes in C++