Home » Bitpay conversions using PHP

Bitpay conversions using PHP

by hotshed71

In this example we will display some conversions using bitpays api. the url we are interested in is located at https://bitpay.com/api/rates

This is abbreviated output
[
{“code”:”BTC”,”name”:”Bitcoin”,”rate”:1},
{“code”:”BCH”,”name”:”Bitcoin Cash”,”rate”:6.327061},
{“code”:”USD”,”name”:”US Dollar”,”rate”:9095.53},
{“code”:”EUR”,”name”:”Eurozone Euro”,”rate”:7609.91},
{“code”:”GBP”,”name”:”Pound Sterling”,”rate”:6698.966991},
{“code”:”JPY”,”name”:”Japanese Yen”,”rate”:998393.589275},

Th example below will display conversions but can be affected if the api changes, in the example below you will see lines of code like the following

$rateUSD = $data[2][“rate”];

which corresponds to

{“code”:”USD”,”name”:”US Dollar”,”rate”:9095.53},

Obviously if bitpay were to change this it would display incorrect results

Code

[php]

<?php
$url = “https://bitpay.com/api/rates”;
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
$rateUSD = $data[2][“rate”];
$rateBCH = $data[1][“rate”];
$rateEUR = $data[3][“rate”];
$rateGBP = $data[4][“rate”];
?>
BTC/USD: <?=$rateUSD?>
<br>
BTC/Bitcoin Cash: <?=$rateBCH?>
<br>
BTC/EUR: <?=$rateEUR?>
<br>
BTC/GBP : <?=$rateGBP?>

[/php]

 

Output

You should see something like this

BTC/USD: 9095.03
BTC/Bitcoin Cash: 6.326933
BTC/EUR: 7610.18
BTC/GBP : 6698.598735

You may also like