PHP Function to Get Exchange Rate Between Cryptocurrency (BTC, LTC, ETH) to Fiat Currency?


bitcoin-exchange PHP Function to Get Exchange Rate Between Cryptocurrency (BTC, LTC, ETH) to Fiat Currency? blockchain Coinbase API Cryptocurrency php

bitcoin-exchange

The Cryptocurrencies such as Bitcoins, Litecoins, ETH coins are the top coins among others. The exchange rate to Fiat currency may fluctuate greatly many times per day. We may want to know the exchange rate at any time based on the API provided by the Cryptocurrency exchange e.g. coinbase.

The coinbase provides a free API that is currently rate-limited at 10,000 calls per hour. You don’t need the OAuth or authentication to use the API. The only requirement is set the header parameter CB_VERSION as the format of DATE YYYY-MM-DD.

The following wraps the API using the shell_exec function in PHP, which runs the external command curl.

1
2
3
4
5
6
7
8
9
10
11
function GetPrice($from, $to) {
  $from = (trim(strtoupper($from)));
  $to = (trim(strtoupper($to)));
  $url = 'curl -s -H "CB-VERSION: 2017-12-06" "https://api.coinbase.com/v2/prices/'.$from.'-'.$to.'/spot"';
  $tmp = shell_exec($url);
  $data = json_decode($tmp, true);
  if ($data && $data['data'] && $data['data']['amount']) {
    return (float)$data['data']['amount'];
  }
  return null;
}
function GetPrice($from, $to) {
  $from = (trim(strtoupper($from)));
  $to = (trim(strtoupper($to)));
  $url = 'curl -s -H "CB-VERSION: 2017-12-06" "https://api.coinbase.com/v2/prices/'.$from.'-'.$to.'/spot"';
  $tmp = shell_exec($url);
  $data = json_decode($tmp, true);
  if ($data && $data['data'] && $data['data']['amount']) {
    return (float)$data['data']['amount'];
  }
  return null;
}

For example, you can get the exchange rate between BTC to USD simply calling:

1
print_r(GetPrice("BTC", "USD"));
print_r(GetPrice("BTC", "USD"));

The following is a handy report to know your favorite coins better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function GetReport($from) {
  $s = '';
  $from = trim(strtoupper($from));
  $gbp = GetPrice($from, 'GBP');
  $eur = GetPrice($from, 'EUR');
  $usd = GetPrice($from, 'USD');
  $cny = GetPrice($from, 'CNY');
  if ($cny) {
    $s .= "1 $from = ¥$cny\n";
  }  
  if ($usd) {
    $s .= "1 $from = \$$usd\n";
  }
  if ($gbp) {
    $s .= "1 $from = £$gbp\n";
  }
  if ($eur) {
    $s .= "1 $from = €$eur\n";
  }   
  return $s;
}
function GetReport($from) {
  $s = '';
  $from = trim(strtoupper($from));
  $gbp = GetPrice($from, 'GBP');
  $eur = GetPrice($from, 'EUR');
  $usd = GetPrice($from, 'USD');
  $cny = GetPrice($from, 'CNY');
  if ($cny) {
    $s .= "1 $from = ¥$cny\n";
  }  
  if ($usd) {
    $s .= "1 $from = \$$usd\n";
  }
  if ($gbp) {
    $s .= "1 $from = £$gbp\n";
  }
  if ($eur) {
    $s .= "1 $from = €$eur\n";
  }   
  return $s;
}

The above GetReport has been implemented in the Wechat Subscription Bot.

btc-ltc-eth-lookup-wechat PHP Function to Get Exchange Rate Between Cryptocurrency (BTC, LTC, ETH) to Fiat Currency? blockchain Coinbase API Cryptocurrency php

PHP Function calls the CoinBase API to know the price!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
446 words
Last Post: SteemSQL Tutorial: How to Avoid SQL Injection?
Next Post: Quick Tutorial to 64-bit Tablacus Scripting Control

The Permanent URL is: PHP Function to Get Exchange Rate Between Cryptocurrency (BTC, LTC, ETH) to Fiat Currency?

Leave a Reply