How to get price from bitcoin to USD with api

0 votes

Please help me how to get the price from bitcoin to USD with bitpay API. I wrote the following code, but I got USD to bitcoin price. I want bitcoin to USD price. Thanks!

<?php 

$url = "https://bitpay.com/api/rates"; 

$json = file_get_contents($url); 
$data = json_decode($json, TRUE); 

$rate = $data[1]["rate"]; 
$usd_price = 10; # Let cost of elephant be 10$ 

$bitcoin_price = round( $usd_price / $rate , 8 ); 

?>

Apr 5, 2022 in Blockchain by Soham
• 9,700 points
2,486 views

1 answer to this question.

0 votes

If I'm not wrong, is this what you mean?

$url='https://bitpay.com/api/rates'; 
$json=json_decode( file_get_contents( $url ) ); 
$dollar=$btc=0; 

foreach( $json as $obj ){ 
      if( $obj->code=='USD' )$btc=$obj->rate; 
} 

echo "1 bitcoin=\$" . 
$btc . "USD<br />"; $dollar=1 / $btc; 
echo "10 dollars = " . round( $dollar * 10,8 )."BTC";

The Returns:

1 bitcoin=$11485USD 
10 dollars = 0.0008707BTC

The best way to become a Blockchain concept is by getting courses in blockchain technology.

answered Apr 7, 2022 by Aditya
• 7,680 points

Related Questions In Blockchain

0 votes
1 answer

How To Get BTC Price on Binance from Binance API

In the Script environment for Apps, the ...READ MORE

answered Feb 28, 2022 in Blockchain by Soham
• 9,700 points
2,023 views
0 votes
1 answer

How to get historic Bitcoin Price Index using coinbase or any other open source API?

I would recommend you to use the ...READ MORE

answered Apr 7, 2022 in Blockchain by Rahul
• 9,670 points
1,067 views
0 votes
0 answers

How to get AUD price of bitcoin at a particular date

I am trying to find out how ...READ MORE

Mar 1, 2022 in Blockchain by Soham
• 9,700 points
279 views
0 votes
0 answers

How to get Bitcoin value for corresponding USD value in ASP.NET C#?

I want to get the Bitcoin value ...READ MORE

Mar 1, 2022 in Blockchain by Soham
• 9,700 points
667 views
0 votes
1 answer

Cant get a specific value in JSON/PHP

Convert the json-string to a php-array and ...READ MORE

answered Sep 11, 2018 in Blockchain by slayer
• 29,350 points
2,587 views
0 votes
1 answer

jsonRPCClient Error with php variables

All you have to do is change ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,740 points
507 views
0 votes
0 answers

API - Convert Bitcoin Amount into USD with my Api

After apiv2.bitcoinaverage.com is not free anymore I ...READ MORE

Mar 2, 2022 in Blockchain by Aditya
• 7,680 points
847 views
0 votes
0 answers

Fatal error: Maximum execution time of 30 seconds exceeded

I am getting this error when I ...READ MORE

May 16, 2022 in PHP by Kichu
• 19,050 points
540 views
0 votes
1 answer

How to get Bitcoin Private Key from an ExtPrivKey using NBitcoin

From what I see in the code ...READ MORE

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

BITCOIN TO USD API

You can use blockchain.info API: URL: https://blockchain.info/ticker Response: { "USD" : ...READ MORE

answered Apr 6, 2022 in Blockchain by Aditya
• 7,680 points
451 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