Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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