tonnylaf
tonnylaf

Reputation: 13

API - Convert Bitcoin Amount into USD with my Api

After apiv2.bitcoinaverage.com is not free anymore i need an other solution.

I want convert an x BTC Amount into USD from current rate.

My Old Code:

$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);

Can i do the same with api from https://alternative.me/crypto/api/ ?

Many Thanks

Upvotes: 1

Views: 3627

Answers (1)

jibsteroos
jibsteroos

Reputation: 1391

You can do following, according to the documentation:

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

note(thx @AndreasHassing): if you only want Bitcoin data in the json response, use:

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

Upvotes: 3

Related Questions