Background:
I've created a dynamic website where lots of the content is generated by RSS feeds from themoneyconvert.com
The website displays live currency rates like such:
Hopefully you get the idea of the contents I'm displaying across a 3 column template.
The feed URL's to themoneyconverter.com are set up in a script that I've called cityConfig.php
<?php
// Feed URL's //
$theMoneyConverter = 'http://themoneyconverter.com/rss-feed/';
// Define arrays //
$cities = array('London', 'New York', 'Paris');
$currencySource = array($theMoneyConverter . 'GBP/rss.xml?x=15', $theMoneyConverter . 'USD/rss.xml?x=16', $theMoneyConverter . 'EUR/rss.xml?x=56');
?>
The feed URL's are stored in the $currencySource
array. I have added an argument onto the end of each URL. For example, the first item in the array has ?x=15
added onto the end of the existing feed. This argument corresponds to the position of the <item>
XML tag from the feed URL.
The tag is accessed by the following line of code which is inside a function that will displayed when I get to it.
$currency['rate'] = $xml->channel->item[$x]->description;
Notice the $x
variable above which I'm passing the argument into.
The following functions are located in my getCurrencyRate.php
script.
<?php
// Get XML data from source
// Check feed exists
function get_currency_xml($currencySource) {
if (isset($currencySource)) {
$feed = $currencySource;
} else {
echo 'Feed not found. Check URL';
}
if (!$feed) {
echo('Feed not found');
}
return $feed;
}
function get_currency_rate($feed) {
$xml = new SimpleXmlElement($feed);
$rate = get_rate($xml, 15); //EUR 15
if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16') {
$rate = get_rate($xml, 16); //GBP 16
} else {
$rate = get_rate($xml, 56); //USD 56
}
}
Notice above that I have hard coded the values 15, 16 and 56
The output from this can be viewed in the first image at the top of the post. What I am trying to do is get these values parsed in from the argument set in the feed as shown in cityConfig.php
script.
The get_rate
function above calls the following:
// Get and return currency rate
// Perform regular expression to extract numeric data
// Split title string to extract currency title
function get_rate(SimpleXMLElement $xml, $x) {
$x = (int)$x;
$currency['rate'] = $xml->channel->item[$x]->description;
preg_match('/([0-9]+\.[0-9]+)/', $currency['rate'], $matches);
$rate = $matches[0];
$title['rate'] = $xml->channel->item[$x]->title;
$title = explode('/', $title['rate']);
$title = $title[0];
echo $rate . ' ' . $title . '<br />';
}
To achieve my goal I have altered the get_currency_rate
function from above by adding the following lines of code and replacing the numeric value to variable $x
.
$vars = parse_url($feed, PHP_URL_QUERY);
parse_str($vars);
and the modified function:
function get_currency_rate($feed) {
$xml = new SimpleXmlElement($feed);
$vars = parse_url($feed, PHP_URL_QUERY);
parse_str($vars);
$rate = get_rate($xml, $x); //EUR 15
if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16') {
$rate = get_rate($xml, $x); //GBP 16
} else {
$rate = get_rate($xml, $x); //USD 56
}
}
The output from the above displays:
I am expecting the same output in the columns as before but this one is different. Any ideas where I've gone wrong?
Thanks in advance