Skip to content

Instantly share code, notes, and snippets.

@timvisee
Last active August 6, 2022 16:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timvisee/b19cf89d29455570e0b7 to your computer and use it in GitHub Desktop.
Save timvisee/b19cf89d29455570e0b7 to your computer and use it in GitHub Desktop.
Live currency exchange script, to retrieve the current exchange rates betwee to currencies, live from the internet.
<?php
/**
* Live currency exchange script.
* This script allows you to retrieve the current currency exchanges, live, from the internet.
*
* This script is developed by Tim Visee, for educational purposes.
* The Yahoo currency exchange API is used to retrieve the live exchange rates.
*
* ---
*
* Script usage:
* To retrieve the current currency exchange rates for two currencies, simply make a request to this script with the
* parameters that are described bellow.
*
* Script parameters:
* - from: Three letter code for base currency.
* - to: Three letter code for target currency.
*
* For example, to get the exchange rate for EUR to USD, use the following parameters:
* index.php?from=EUR&to=USD
* The currency exchange rate is returned as a numerical value, with a dot as decimal delimiter.
*
* The string 'ERROR' is returned if an error occurred. For example, if the current exchange rate couldn't be retrieved,
* or if the three letter currency code is invalid.
*
* ---
*
* @author Tim Visee
* @website http://timvisee.com/
* @copyright Copyright (c) Tim Visee 2015. All rights reserved.
*/
// Disable all PHP errors
error_reporting(0);
ini_set('display_errors', 0);
/**
* Show an error, and stop the script.
*/
function showError() {
die('ERROR');
}
// Make sure the currencies are set
if(!isset($_GET['from']) || !isset($_GET['from']))
showError();
// Get the currencies to convert
$from = trim(strtoupper($_GET['from']));
$to = trim(strtoupper($_GET['to']));
// Make sure both currency identifiers are three characters long
if(strlen($from) != 3 || strlen($to) != 3)
showError();
// Create the conversion string
$conversionString = $from . $to;
// Do the whole exchange rate retrieval process in a try-catch block to handle possible errors
try {
// Get the current exchange
$url = "http://query.yahooapis.com/v1/public/yql?env=store://datatables.org/alltableswithkeys&q=SELECT%20*%20FROM%20yahoo.finance.xchange%20WHERE%20pair%20IN%20%28%22" . $conversionString . "%22%29";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$xml = curl_exec($ch);
// Parse the XML data
$xmlData = new SimpleXMLElement($xml);
// Make sure the rate is set
if(!isset($xmlData->results->rate->Rate))
showError();
// Get and parse the exchange rate as a float
$exchangeRate = floatval($xmlData->results->rate->Rate);
// Echo the actual exchange rate
echo $exchangeRate;
} catch(Exception $e) {
// Show an error page
showError();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment