PHP Bitcoin to Ukrainian Hrivna price ticker

0 votes

I am trying to create a real time tiker of the price of bitcoin from the BTC-E.com exchange in Ukrainian currency. The end result is to put the price on my simple html website. The exchange does not provide the price in hrivna (UAH), so i have to convert from BTC to USD to UAH.

I am using the api provided by BTC-E

https://btc-e.com/api/2/btc_usd/ticker

this is the php file i have created

<?php
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

$data = file_get_contents("https://btc-e.com/api/2/btc_usd/ticker");
$data = json_decode($data, true);
$spot_last = $data['ticker']['last'];
echo $spot_last;
?>    

and this is the code i put in index.html

<script>
     var auto_refresh = setInterval(
     function()
     {$('.btce_price').load('ticker.php');}, 1000);


  </script>

In order to have USD to UAH conversion i would like to use the commercial rate from one of the banks.

 https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=5

whis returns the following

<exchangerates>
<row>
<exchangerate ccy="RUR" base_ccy="UAH" buy="0.25000" sale="0.28000"/>
</row>
<row>
<exchangerate ccy="EUR" base_ccy="UAH" buy="13.30000" sale="14.30000"/>
</row>
<row>
<exchangerate ccy="USD" base_ccy="UAH" buy="9.60000" sale="10.10000"/>
</row>
</exchangerates>

so i have the other php file to output the buy rate that i need

$xml = simplexml_load_file("https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=5");
$m = $xml->xpath('//exchangerate[@ccy="USD"]');
$exrate = (string)$m[0]['buy'];
echo $exrate;

Now I want to knwo how to divide the output of the first file buy the second? Need help

Sep 11, 2018 in Blockchain by digger
• 26,740 points
920 views

1 answer to this question.

0 votes

nstead of dividing the values - i would multiply them.

Actually: 1 BTC = 633 USD and 633 USD =~ 6000 UAH. So i would calculate: $uah = $spot_last * $exrate;

Example

<?php

header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.

// get BTC
$data = file_get_contents("https://btc-e.com/api/2/btc_usd/ticker");
$data = json_decode($data, true);
$spot_last = $data['ticker']['last'];

echo '1 BTC is worth ' . $spot_last . ' USD. <br>';

// get CURRENCIES
$xml = simplexml_load_file("https://api.privatbank.ua/p24api/pubinfo?exchange&coursid=5");
$m = $xml->xpath('//exchangerate[@ccy="USD"]');
$exrate = (string)$m[0]['buy'];

echo '1 USD is worth '. $exrate .' UAH. <br>';

$uah = $spot_last * $exrate;

echo 'Result: 1 BTC ~ ' . $uah . ' UAH. <br>';

For usage as ticker, delete all echos statements, they are just for demonstration and insert just echo $uah;

Output

1 BTC is worth 624.928 USD.
1 USD is worth 9.60000 UAH.
1 BTC ~ 5999.3088 UAH. 
answered Sep 11, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

0 votes
1 answer

PHP. Bitcoin to Ukrainian Hrivna price ticker

I would suggest, that instead of dividing ...READ MORE

answered Apr 7, 2022 in Blockchain by Aditya
• 7,680 points
606 views
0 votes
1 answer

How to use bitcoin price as place holder in php?

You can do it by providing an ...READ MORE

answered Aug 21, 2018 in Blockchain by digger
• 26,740 points
926 views
0 votes
1 answer

How to create Bitcoin Address in PHP?

You need to understand that Bitcoin address and ...READ MORE

answered Aug 28, 2018 in Blockchain by Perry
• 17,100 points
5,451 views
+1 vote
1 answer
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
45,219 views
0 votes
1 answer

How do I add multiple recipients for transactions via Blockchain API?

Convert the recipes into JSON objects. x = ...READ MORE

answered Jul 6, 2018 in Blockchain by Perry
• 17,100 points
885 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
2 answers

Check Bitcoin Balance with mybitx api https GET url

You can use the PHP Requests library for an ...READ MORE

answered Sep 17, 2018 in Blockchain by Lalit
2,541 views
0 votes
1 answer

Php code to generate Ethereum wallets.

You can use web3.eth.accounts in web3js web3.eth.accounts.create(); With Php, ...READ MORE

answered Jul 27, 2018 in Blockchain by slayer
• 29,370 points
3,368 views
0 votes
1 answer

How to solve “insufficient funds for gas * price + value” error?

Change the following line Credentials credentials = Credentials.create(firstAccount); To ...READ MORE

answered Aug 1, 2018 in Blockchain by slayer
• 29,370 points
5,708 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP