API - 使用我的 API 将比特币金额转换为美元



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 做同样的事情吗?

非常感谢

根据文档,您可以执行以下操作:

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

注意(Thx @AndreasHassing(:如果您只想在 JSON 响应中使用比特币数据,请使用:

$getrate = "https://api.alternative.me/v2/ticker/bitcoin/?convert=USD";

最新更新