API-使用我的Api将比特币金额转换为USD

时间:2020-01-13 10:51:54

标签: php json

apiv2.bitcoinaverage.com不再免费之后,我需要其他解决方案。

我想将x BTC Amount从当前汇率转换为美元。

我的旧代码:

$getrate = "https://apiv2.bitcoinaverage.com/convert/global?from=BTC&to=USD&amount=0.005";
$btcprice = array(
  'price' =>
    array(
    'method'  => 'GET',
    )
);
$priceone = stream_context_create($btcprice);
$pricetwo = file_get_contents($getrate, false, $priceone);
$result = json_decode($pricetwo, true);

我可以对来自https://alternative.me/crypto/api/的api进行同样的操作吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

您可以按照documentation进行以下操作:

image:

注释(thx @AndreasHassing):如果您只想在json响应中使用比特币数据,请使用:

<?php $getrate = "https://api.alternative.me/v2/ticker/?convert=USD"; $price = file_get_contents($getrate); $result = json_decode($price, true); // BTC in USD $result = $result['data'][1]['quotes']['USD']['price']; $quantity = 0.005; $value = $quantity * $result; echo 'value : ' . $value;