Question

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:

enter image description here

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:

enter image description here

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

Était-ce utile?

La solution

Look at your code in your first get_currency_rate function.

    $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
    }

Let's examine what it does execute. Either this,

    $rate = get_rate($xml, 15); //EUR 15
    $rate = get_rate($xml, 16); //GBP 16

or this,

    $rate = get_rate($xml, 15); //EUR 15
    $rate = get_rate($xml, 56);  //USD 56

Now. Consider what your new get_currency_rate function will actually execute.

    $vars = parse_url($feed, PHP_URL_QUERY);
    parse_str($vars);
    # This will mean $x = 15, 16 or whatever. This will be depending of your $feed.
    # If your $feed **is** http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16
    # It will mean $x = 16 and the following code will be executed.

    $rate = get_rate($xml, 16); //EUR 15 # $x = 16
    if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16') {
        $rate = get_rate($xml, 16); //GBP 16 # $x = 16
    }

Or,

    $vars = parse_url($feed, PHP_URL_QUERY);
    parse_str($vars);
    # If your $feed **is** http://themoneyconverter.com/rss-feed/USD/rss.xml?x=15
    # It will mean $x = 15 and the following code will be executed.

    $rate = get_rate($xml, 15); //EUR 15 # $x = 15
    if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16') {
    } else {
        $rate = get_rate($xml, 15); //USD 56 # $x = 15
    }

So, basically what you are running are allways two identical calls to get_rate.

Like so,

    $rate = get_rate($xml, 15); //EUR 15 # $x = 15
    $rate = get_rate($xml, 15); //USD 56 # $x = 15

I trust by now, you can spot the error. They will both result in the same line beeing printed out.

    0.76429 EUR
    0.76429 EUR

As a solution, I would suggest a switch-case construction somewhat like the followng:

    function get_currency_rate($feed) {
        $xml = new SimpleXmlElement($feed);
        $vars = parse_url($feed, PHP_URL_QUERY);
        parse_str($vars);
        switch ($x) {
            case 15:
                get_rate($xml, 16); //GBP 16
                get_rate($xml, 56); //USD 56
                break;
            case 16:
                get_rate($xml, 15); //EUR 15
                get_rate($xml, 56); //USD 56
                break;
            case 56: default:
                get_rate($xml, 15); // EUR 15
                get_rate($xml, 16); // GBP 16
                break;
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top