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> </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
PHP - Web2 and caching [1633] - ()
[1647] - ()
[1733] - ()
[1812] - ()
[1813] - ()
[1814] - ()
[1926] - ()
[1995] - ()
[2196] - ()
[2321] - ()
[2545] - ()
[3029] - ()
[3094] - ()
[3186] - ()
[3458] - ()
[3955] - ()
[3999] - ()
[4055] - ()
[4075] - ()
[4106] - ()
[4136] - ()
[4627] - ()
PHP - Further Web Page and Network Handling [220] - ()
[314] - ()
[345] - ()
[356] - ()
[372] - ()
[376] - ()
[410] - ()
[425] - ()
[443] - ()
[451] - ()
[484] - ()
[537] - ()
[542] - ()
[565] - ()
[603] - ()
[675] - ()
[767] - ()
[789] - ()
[847] - ()
[904] - ()
[936] - ()
[1009] - ()
[1114] - ()
[1183] - ()
[1187] - ()
[1210] - ()
[1355] - ()
[1379] - ()
[1485] - ()
[1495] - ()
[1496] - ()
[1505] - ()
[1515] - ()
[1518] - ()
[1549] - ()
[2632] - ()
[2679] - ()
[2729] - ()
[2918] - ()
[3036] - ()
[3432] - ()
[3540] - ()
[3568] - ()
[3918] - ()
[4070] - ()
[4483] - ()
Web Application Design and Deployment [23] - ()
[356] - ()
[443] - ()
[659] - ()
[767] - ()
[1198] - ()
[1256] - ()
[1351] - ()
[1545] - ()
[1547] - ()
[1798] - ()
[2072] - ()
[3532] - ()
Web Application Deployment - Commercial and Legal Aspects [142] - ()
[259] - ()
[288] - ()
[320] - ()
[322] - ()
[532] - ()
[746] - ()
[759] - ()
[795] - ()
[876] - ()
[994] - ()
[1054] - ()
[1431] - ()
[1485] - ()
[1486] - ()
[1506] - ()
[1747] - ()
[1840] - ()
[1937] - ()
[2140] - ()
[2252] - ()
[2592] - ()
[2686] - ()
[3016] - ()
[3104] - ()
[3168] - ()
[3745] - ()
[3746] - ()
[3747] - ()
[4234] - ()
[4283] - ()
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.