Skip to content

Instantly share code, notes, and snippets.

@lucablackwell
Last active November 29, 2022 09:55
Show Gist options
  • Save lucablackwell/bd2717f89ae707cb10cfcf18c176abc6 to your computer and use it in GitHub Desktop.
Save lucablackwell/bd2717f89ae707cb10cfcf18c176abc6 to your computer and use it in GitHub Desktop.
<?php
function white_bold($text) {
return "\e[1;37m$text\e[0m";
}
function black($text) {
return "\e[1;30m$text\e[0m";
}
function yellow($text) {
return "\e[0;33m$text\e[0m";
}
# Show and take input for choices
function show_choice($choices) {
# For each choice
foreach ($choices as $choice) {
echo " ";
echo(white_bold(str_pad($choice[0], 5)) . black(" | ") . yellow($choice[1]) . "\n");
}
# Take user input
$input = readline('> ');
$forward = false;
$to_return = null;
while (!$forward) {
# Loop through each choice
foreach ($choices as $choice) {
# If the input is the same as the current choice
if (strtolower($input) == strtolower($choice[0]) || strtolower($input) == strtolower($choice[1])) {
# Set the current choice to be returned
$to_return = $choice[0];
$forward = true;
}
}
# If there isn't something to return (i.e. the input is invalid)
if (!$to_return) {
echo("\nInvalid input." . "\n");
# Take user input again
$input = readline('> ');
}
}
# Return the assigned value
return $to_return;
}
$real = file('currencies.csv');
$real_arr = [];
$both_arr = [];
foreach ($real as $line) {
$line_split = explode(',', $line);
$real_arr[] = [$line_split[0], trim($line_split[1])];
$both_arr[] = [$line_split[0], trim($line_split[1])];
}
$crypto = file('crypto.csv');
$crypto_arr = [];
foreach ($crypto as $line) {
$line_split = explode(',', $line);
$crypto_arr[] = [$line_split[0], trim($line_split[1])];
$both_arr[] = [$line_split[0], trim($line_split[1])];
}
$first = show_choice($both_arr);
$second = show_choice($real_arr);
echo "Retrieving $first -> $second...\n";
$json = file_get_contents('https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=' . $first . '&to_currency=' . $second . '&apikey=M1EQSI46GV1IBL2I');
$response = json_decode($json,true);
if (array_key_exists('Error Message', $response) && $response['Error Message'] == 'Invalid API call. Please retry or visit the documentation (https://www.alphavantage.co/documentation/) for CURRENCY_EXCHANGE_RATE.') {
exit(white_bold("This conversion is currently unavailable.\n"));
} elseif (array_key_exists('Note', $response) && $response['Note'] == 'Thank you for using Alpha Vantage! Our standard API call frequency is 5 calls per minute and 500 calls per day. Please visit https://www.alphavantage.co/premium/ if you would like to target a higher API call frequency.') {
exit(white_bold("Limit reached. Please wait a moment before trying again.\n"));
}
$response = $response['Realtime Currency Exchange Rate'];
echo "Retrieved:\n";
echo "$first -> $second: " . yellow($response['5. Exchange Rate']) . "\n";
echo "Refreshed: " . yellow($response['6. Last Refreshed'] . ' (' . $response['7. Time Zone']) . ")\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment