Last active
July 9, 2017 14:05
-
-
Save NickHatBoecker/6414992ec6352c38e10f3e7139196935 to your computer and use it in GitHub Desktop.
PHP Script for macOS crypto currencies widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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