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 in Blockchain by Soham
• 8,730 points
103 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

answered Apr 7 by Aditya
• 7,280 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 in Blockchain by Soham
• 8,730 points
216 views
0 votes
1 answer
0 votes
0 answers
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 in Blockchain by Soham
• 8,730 points
99 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,310 points
1,933 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,720 points
197 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 in Blockchain by Aditya
• 7,280 points
171 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 in PHP by Kichu
• 19,040 points
62 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 in Blockchain by Aditya
• 7,280 points
164 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 in Blockchain by Aditya
• 7,280 points
32 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP