RE: Currencies Maintenance: graphs no longer work
Below is my yahoo function. It would be really nice if anybody knows of an open data source for this information where the APIs wouldn't get arbitrarily removed or changed regularly.
Tim
function yahoo_currency_rate($CurrCode) {
if (http_file_exists('https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote')) {
$xml = file_get_contents('https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote');
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $xml, $tags);
xml_parser_free($parser);
$elements = array(); // the currently filling [child] XmlElement array
$stack = array();
foreach ($tags as $tag) {
$index = count($elements);
if ($tag['type'] == 'complete' or $tag['type'] == 'open') {
$elements[$index] = new XmlElement;
$elements[$index]->name = $tag['tag'];
if (isset($tag['attributes'])) {
$elements[$index]->attributes = $tag['attributes'];
}
if (isset($tag['value'])) {
$elements[$index]->content = $tag['value'];
}
if ($tag['type'] == 'open') { // push
$elements[$index]->children = array();
$stack[count($stack)] =& $elements;
$elements =& $elements[$index]->children;
}
}
if ($tag['type'] == 'close') { // pop
$elements =& $stack[count($stack) - 1];
unset($stack[count($stack) - 1]);
}
}
$Currencies = array();
foreach ($elements[0]->children[1]->children as $CurrencyDetails) {
foreach ($CurrencyDetails as $CurrencyDetail) {
if (is_array($CurrencyDetail) and isset($CurrencyDetail[0])) {
$Currencies[mb_substr($CurrencyDetail[0]->content, 4)] = $CurrencyDetail[1]->content;
}
}
}
$Currencies['USD'] = 1; //ECB delivers no rate for Euro
//return an array of the currencies and rates
return $Currencies;
} else {
return false;
}
}
|