Skip to content

Instantly share code, notes, and snippets.

@f13dev
Created July 13, 2020 05:51
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 f13dev/92da912f56d67ad795c3d5a34d9cb2cc to your computer and use it in GitHub Desktop.
Save f13dev/92da912f56d67ad795c3d5a34d9cb2cc to your computer and use it in GitHub Desktop.
<?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