Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function currencyConvert($from,$amount,$to) {
// Initiate curl
$curl = curl_init();
// Set the curl URL and options
curl_setopt($curl, CURLOPT_URL, '' . $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
// 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