Skip to content

Instantly share code, notes, and snippets.

@NickHatBoecker
Last active July 9, 2017 14:05
Show Gist options
  • Save NickHatBoecker/6414992ec6352c38e10f3e7139196935 to your computer and use it in GitHub Desktop.
Save NickHatBoecker/6414992ec6352c38e10f3e7139196935 to your computer and use it in GitHub Desktop.
PHP Script for macOS crypto currencies widget
<?php
/*
* Author: Nick Böcker
* Created: 09.07.2017
* Homepage: https://nick-hat-boecker.de
*/
class CryptoCoins
{
private $curl;
private $apiBaseUrl = 'https://min-api.cryptocompare.com/data/price';
// #### EDIT HERE ####
// Currencies you want to check
private $currencies = array('STEEM', 'BTC', 'ETH', 'LTC');
// Currencies to convert to
private $targetCurrencies = array('USD', 'EUR');
// #### STOP EDITING ####
public function run()
{
$this->curl = curl_init();
$this->renderPrices();
curl_close($this->curl);
}
private function renderPrices()
{
echo "----\n";
foreach ($this->currencies as $currency) {
$apiUrl = $this->apiBaseUrl.'?fsym='.$currency.'&tsyms='.implode(',', $this->targetCurrencies);
$price = $this->getPrices($apiUrl);
echo $currency.': '.$this->getPriceString($price)."\n----\n";
}
}
/**
* @param string $apiUrl
*
* @return array
*/
private function getPrices($apiUrl)
{
curl_setopt($this->curl, CURLOPT_URL, $apiUrl);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
return json_decode(curl_exec($this->curl), true);
}
/**
* @param array $prices
*
* @return string
*/
private function getPriceString($prices)
{
$strings = array();
foreach ($prices as $currency => $price) {
$strings[] = $currency.': '.$price;
}
return implode('; ', $strings);
}
}
$cryptoCoins = new CryptoCoins();
$cryptoCoins->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment