简体   繁体   English

如何使用API​​将比特币价格转换为USD

[英]How to get price from bitcoin to USD with api

Please help me how to get the price from bitcoin to USD with bitpay API. 请帮助我如何使用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 );
?>

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";

returns 回报

1 bitcoin=$11485USD
10 dollars = 0.0008707BTC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM