Created
July 13, 2020 05:51
-
-
Save f13dev/92da912f56d67ad795c3d5a34d9cb2cc to your computer and use it in GitHub Desktop.
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 | |
function currencyConvert($from,$amount,$to) { | |
// Initiate curl | |
$curl = curl_init(); | |
// Set the curl URL and options | |
curl_setopt($curl, CURLOPT_URL, 'https://api.exchangeratesapi.io/latest?base=' . $from); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
// Get the output from the curl request | |
$output = curl_exec($curl); | |
// Decode the output | |
$conversion = json_decode($output); | |
// Close the curl connection | |
curl_close($curl); | |
// Return the converted amount | |
return $amount * $conversion->{'rates'}->{$to}; | |
} | |
echo currencyConvert('USD', 51.28, 'GBP'); // Shows $51.28 in GBP (£40.73 at the time of writing) | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment