Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Using current exchange rates on a web page

It's one thing writing a web site and quite another keeping it up to date - especially when the information to be maintained changes on a daily basis and it's not your main business.

One example of changing data of this type is the exchange rate between currencies, an issue that we faced on our own and on other sites we work on. We solved it by taking a feed from the European Central Bank who make the information available in a file that they update daily.

The code is written in PHP, and reproduced below.

You may try it here You may be taught how to do it here You may ask about it here
<html><head><title>Current Exchange Rate Demo</title><</head>
<body><h2>Exchange rate demo</h2>
This demonstration program shows you how you can grab a file from
another web site (in this example, the European Central Bank) and
use it within your own page. Exchange rates are updated daily;
using the code in this page, we can maintain our information
automatically.<br><br>
Caution - this is an example. We are not responsible for the
rates quoted which you MUST check yourself. You must also check
that you are allowed by law to grab information of another web
site before you do so in your own application.
The <a href=http://www.ecb.int>ECB site</a> is declared as a
feed of public information, but that doesn't apply to many sites
and you'll need to check copyright issues, etc.<br><br>
<?php

$now = time();
$cache = "$_SERVER[DOCUMENT_ROOT]/../include/erates.xml";
$exchrate[EUR] = 1.00;
$amount = 1000;

# Exchange rates - Grab current rates from European Central Bank

# Check whether we have a recent copy of the data locally

if (($interval = $now - filemtime($cache)) > 3600 * 2) {
 $stuff = file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
 $trace = "Cache REFRESHED after $interval seconds";
 $fh = fopen ($cache,"w");
 foreach ($stuff as $line) {
  fputs($fh,$line);
  }
} else {
 $stuff = file($cache);
 $trace = "Cached information reused aged $interval seconds";
 }

# Currency names that we'll use later on

$names = array (USD => "US Dollar",
        JPY => "Japanese Yen",
        DKK => "Danish Krone",
        GBP => "Pound Sterling",
        SEK => "Swedish Krona",
        CHF => "Swiss Franc",
        ISK => "Icelandic Krona",
        NOK => "Norwegian Krone",
        BGN => "Bulgarian Lev",
        CYP => "Cyprus Pound",
        CZK => "Czech Koruna",
        EEK => "Estonian Kroon",
        HUF => "Hungarian Forint",
        LTL => "Lithuanian Litas",
        LVL => "Latvian Lats",
        MTL => "Maltese Lira",
        PLN => "Polish Zloty",
        ROL => "Romanian Leu",
        SIT => "Slovenian Tolar",
        SKK => "Slovakian Koruna",
        TRL => "Old Turkish Lira (to 2004)",
        TRY => "New Turkish Lira (2005)",
        AUD => "Australian Dollar",
        CAD => "Canadian Dollar",
        HKD => "Hong Kong Dollar",
        NZD => "New Zealand Dollar",
        SGD => "Singapore Dollar",
        KRW => "South Korean Won",
        EUR => "European Euro",
        ZAR => "South African Rand");

# Extract data from page - conversion rates between each currency and the Euro

foreach ($stuff as $line) {
        ereg("currency='([[:alpha:]]+)'",$line,$gota);
        if (ereg("rate='([[:graph:]]+)'",$line,$gotb)) {
                $exchrate[$gota[1]] = $gotb[1];
                }
        }

# Sample output - a table showing an amount converted to Euros,
# Pounds and Dollars

print ("<b>Conversion to Euros, Pounds, US Dollars from other currencies</b><br>");
print ("<center><table cellpadding=5><tr><td>&nbsp;</td><th>Euros</th><th>Pounds</th><th>Dollars</th></tr>");
foreach (array_keys($exchrate) as $to) {
        if ($names[$to] == "") $names[$to] = $to;
        print ("<tr><td>$amount $names[$to] is about </td>");
        foreach (array("EUR","GBP","USD") as $from) {
                $convertsto = sprintf("%.2f",$amount * $exchrate[$from] / $exchrate[$to]);
                print ("<td align=right>$convertsto</td>");
                }
        print ("<tr>");
}
print ("</table></center>");
?>
<hr>
Programmer's feedback: <?= $trace ?><br>
Want to <a href=http://www.wellho.net/course/ph.html>learn how to
write a page like this?</a><br>
Copyright <a href=http://www.wellho.net>Well House Consultants</a>,
<?= date("Y") ?>
</body>
</html>


See also PHP Course details

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

PHP - Web2 and caching
  [4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
  [4106] Web server efficiency - saving repetition through caches - (2013-05-30)
  [4075] Further recent PHP examples - (2013-04-28)
  [4055] Using web services to access you data - JSON and RESTful services - (2013-03-29)
  [3999] Handling failures / absences of your backend server nicely - (2013-02-08)
  [3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
  [3458] On this day ... one PHP script with three uses - (2011-09-26)
  [3186] How to add a customised twitter feed to your site - (2011-02-27)
  [3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [2545] Scraping content for your own page via PHP - (2009-12-21)
  [2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
  [2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
  [1995] Automated server heartbeat and health check - (2009-01-16)
  [1926] Flash (client) to PHP (server) - example - (2008-12-06)
  [1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
  [1813] Ajax - going Asyncronous and what it means - (2008-09-28)
  [1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
  [1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
  [1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)

PHP - Further Web Page and Network Handling
  [4070] Passing variable between PHP pages - hidden fields, cookies and sessions - (2013-04-26)
  [3918] Multiple page web applications - maintaining state - PHP - (2012-11-10)
  [3568] Telling which ServerAlias your visitor used - useful during merging domains - (2012-01-04)
  [3540] Easy session example in PHP - keeping each customers data apart - (2011-12-06)
  [3432] 3 digit HTTP status codes - what are they, which are most common, which should be a concern? - (2011-09-11)
  [3036] Sending out an email containing HTML from within a PHP page - (2010-11-07)
  [2918] Downloading a report from the web for further local analysis - (2010-08-13)
  [2729] Uploading a document or image to its own URL via a browser - (2010-04-18)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [2632] Shipping a test harness with your class in PHP - (2010-02-12)
  [1549] http, https and ajp - comparison and choice - (2008-02-22)
  [1518] Downloading data for use in Excel (from PHP / MySQL) - (2008-01-25)
  [1515] Keeping staff up to date on hotel room status - (2008-01-22)
  [1505] Script to present commonly used images - PHP - (2008-01-13)
  [1496] PHP / Web 2 logging - (2008-01-06)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1379] Simple page password protection - PHP - (2007-10-04)
  [1355] .php or .html extension? Morally Static Pages - (2007-09-17)
  [1210] PHP header() function - uses and new restrictions - (2007-05-30)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1183] Improving searches - from OR to AND? - (2007-05-11)
  [1114] PHP Image upload script - (2007-03-21)
  [1009] Passing GET parameters through Apache mod_rewrite - (2006-12-27)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [904] Of course I'll tell you by email - (2006-10-25)
  [847] Image maps for navigation - a straightforward example - (2006-08-28)
  [789] Hot answers in PHP - (2006-07-02)
  [767] Finding the language preference of a web site visitor - (2006-06-18)
  [675] Adding PHP tags to an old cgi program - (2006-04-08)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [565] Using PHP to output images, XML, Style sheets, etc - (2006-01-15)
  [542] Morning image, afternoon image - (2005-12-26)
  [537] Daily Image Santafied - (2005-12-22)
  [484] Setting the file name for a downloaded document - (2005-11-03)
  [451] Accessing a page via POST from within a PHP script - (2005-09-26)
  [443] Server side scripting of styles to suit the browser - (2005-09-12)
  [425] Caching an XML feed - (2005-08-26)
  [410] Reading a news or blog feed (RSS) in your PHP page - (2005-08-12)
  [376] What brings people to my web site? - (2005-07-13)
  [372] Time calculation in PHP - (2005-07-08)
  [356] Sudoku helper or sudoku cheat - (2005-06-23)
  [345] Spotting a denial of service attack - (2005-06-12)
  [314] What language is this written in? - (2005-05-17)
  [220] When to use Frames - (2005-02-19)

Web Application Design and Deployment
  [3532] Sharing the user experience - designing a form with the customer in mind - (2011-11-29)
  [2072] Copyright, Portability and other nontechnical web site issues - (2009-03-09)
  [1798] What does an browser understand? What does an HTML document contain? - (2008-09-15)
  [1547] New bathing idea for hotels from Hotelympia - (2008-02-20)
  [1545] Letting new visitors know we provide training courses - (2008-02-19)
  [1351] Compressing web pages sent out from server. Is it worth it? - (2007-09-14)
  [1256] What country are you in? How we find out on our web site - (2007-07-03)
  [1198] From Web to Web 2 - (2007-05-21)
  [767] Finding the language preference of a web site visitor - (2006-06-18)
  [659] Web Application Components - (2006-03-28)
  [443] Server side scripting of styles to suit the browser - (2005-09-12)
  [356] Sudoku helper or sudoku cheat - (2005-06-23)
  [23] Skills and responsibilities - (2004-08-22)

Web Application Deployment - Commercial and Legal Aspects
  [4283] Can a legitimate forum post become illegal a year later? - (2014-07-11)
  [4234] Change to Libel and Defamation laws from 1st January 2014 - (2013-12-31)
  [3747] An easy way to comply with the new cookie law if your site is well designed - (2012-06-02)
  [3746] Google Analytics and the new UK Cookie law - (2012-06-02)
  [3745] Legal change - You need to obtain user consent if you use cookies on your website - (2012-06-01)
  [3168] Web Sites - Subject to Advertising Standards from 1st March - check your sites - (2011-02-13)
  [3104] Catering in Syracuse, the Saigon Cafe, stolen images and Christmas - (2010-12-25)
  [3016] The legal considerations of your web presence - revisited - (2010-10-26)
  [2686] Freedom of Information - consideration for web site designers - (2010-03-20)
  [2592] Re-using our pictures - (2010-01-21)
  [2252] Leaping dog, Leaping horse, copyright of old masters - (2009-06-20)
  [2140] Beware - giving copyright away when you upload a picture - Dogs Trust - (2009-04-25)
  [1937] Getting hold of the wrong end of the stick - (2008-12-12)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1747] Who is watching you? - (2008-08-10)
  [1506] Ongoing Image Copyright Issues, PHP and MySQL solutions - (2008-01-14)
  [1486] Does anyone understand Lithuanian? - (2007-12-27)
  [1485] Copyright and theft of images, bandwidth and members. - (2007-12-26)
  [1431] Getting the community on line - some basics - (2007-11-13)
  [1054] UK legal requirements for your commercial web site - (2007-01-27)
  [994] Training on Cascading Style Sheets - (2006-12-17)
  [876] Making pages clearer - easy Disability Discrimination Act Compliance - (2006-09-23)
  [795] Remember a site's non-technical issues too - (2006-07-07)
  [759] Watch your Google profile - (2006-06-13)
  [746] Domain Listing Center and Domain Registry of America - (2006-06-06)
  [532] Copyright - how much can I legally copy? - (2005-12-17)
  [322] More maps - (2005-05-23)
  [320] Ordnance Survey - using a 'Get a map' - (2005-05-22)
  [288] Colour blindness for web developers - (2005-04-22)
  [259] Responding to spam - (2005-03-27)
  [142] Colour for access - (2004-12-06)

resource index - PHP
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/php-usin ... -page.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard