请帮我看看如何通过bitpay API从比特币到美元的价格。我写了下面的代码,但我得到了比特币价格的美元。我想比特币到美元价格。谢谢!
<?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 );
?>
答案 0 :(得分:1)
$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";
返回
1 bitcoin=$11485USD
10 dollars = 0.0008707BTC