Skip to content

Instantly share code, notes, and snippets.

@willies952002
Last active August 13, 2016 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willies952002/7b977410fe061efcef9376112d7fb6ce to your computer and use it in GitHub Desktop.
Save willies952002/7b977410fe061efcef9376112d7fb6ce to your computer and use it in GitHub Desktop.
BTC Exchange Rate Checker - A simple script that is used to check the current exchange rate of Bitcoins (BTC)
<?php
/*
* Copyright (c) 2016 William Surgeon (willies952002) <admin@domnian.com>
*
* MIT License:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* BTC Exchange Value Checker
*
* A simple script that is used to check the current exchange rate of Bitcoins (BTC).
*
* You can also supply a number as an argument to check it's value. Defaults to Satashi.
* Suffix with "btc" to convert from BTC.
*/
$data = file_get_contents("https://api.coindesk.com/v1/bpi/currentprice.json");
$json = json_decode($data);
$time = $json->time->updated;
$currencies = $json->bpi;
echo "Current BTC Exchange Rates as of $time:\n";
$usd = 0;
foreach ( $currencies as $code => $data ) {
$sym = html_entity_decode($data->symbol);
$rate = $data->rate_float;
if ($code == "USD") $usd = $rate;
echo "BTC -> $code: $sym$rate\n";
}
$val = false;
if (sizeof($argv) > 1) $val = $argv[1];
if ($val) {
if (endsWith($val, "btc")) {
$btc = substr($val, 0, strlen($val) - 3);
$bal = $btc * $usd;
echo "$btc BTC in USD: $$bal\n";
} else {
$btc = $val / 100000000;
$bal = $btc * $usd;
echo "$val Satoshi ($btc BTC) in USD: $$bal\n";
}
}
function endsWith($haystack, $needle) {
return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false);
}
?>
# php btccheck.php 4605
Current BTC Exchange Rates as of Aug 13, 2016 08:24:00 UTC:
BTC -> USD: $589.0088
BTC -> GBP: £455.8457
BTC -> EUR: €527.634
4605 Satoshi (4.605E-5 BTC) in USD: $0.02712385524
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment