Skip to content

Instantly share code, notes, and snippets.

@pchatterjee
Last active October 29, 2019 18:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pchatterjee/d5239f6e0711e14ba984e149e9520521 to your computer and use it in GitHub Desktop.
<?php
@date_default_timezone_set("GMT");
if (file_exists('rates.xml')) {
copy('rates.xml', 'rates'.'_'.time().'.xml');
$xml = simplexml_load_file('rates.xml');
foreach($xml->rate as $r) {
if ((string) $r['live'] == '1') {
$live[] = (string) $r['code'];
}
}
}
else {
# currency code array
$live = array(
'AUD', 'BRL', 'CAD','CHF',
'CNY', 'DKK', 'EUR','GBP',
'HKD', 'HUF', 'INR','JPY',
'MXN', 'MYR', 'NOK','NZD',
'PHP', 'RUB', 'SEK','SGD',
'THB', 'TRY', 'USD','ZAR'
);
}
# pull the rates json file (USE YOUR OWN API KEY)
$json_rates = file_get_contents('http://data.fixer.io/api/latest?access_key=a7090b272aecc4d3590852c85f3797??')
or die("Error: Cannot load JSON file from fixer");
#decode the json to a php object
$rates = json_decode($json_rates);
# calculate the GBP ratio
$gbp_rate = 1/$rates->rates->GBP;
# start and initialize the writer
$writer = new XMLWriter();
$writer->openURI('rates.xml');
$writer->startDocument("1.0", "UTF-8");
$writer->startElement("rates");
$writer->writeAttribute('base', 'GBP');
$writer->writeAttribute('ts', $rates->timestamp);
foreach ($rates->rates as $code=>$rate) {
$writer->startElement("rate");
$writer->writeAttribute('code', $code);
if ($code=='GBP') {
$writer->writeAttribute('rate', '1.00');
}
else {
$writer->writeAttribute('rate', $rate * $gbp_rate);
}
if (in_array($code, $live)) {
$writer->writeAttribute('live', '1');
}
else {
$writer->writeAttribute('live', '0');
}
$writer->endElement();
}
$writer->endElement();
$writer->endDocument();
$writer->flush();
echo "All done ....!";
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment