3

背景:

我创建了一个动态网站,其中许多内容是由themoneyconvert.com的 RSS 提要生成的

该网站显示实时汇率,如下所示:

在此处输入图像描述

希望您了解我在 3 列模板中显示的内容。

themoneyconverter.com的提要 URL是在我调用的脚本中设置的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');
?>

提要 URL 存储在$currencySource数组中。我在每个 URL 的末尾添加了一个参数。例如,数组中的第一项已?x=15添加到现有提要的末尾。此参数对应于<item>提要 URL 中 XML 标记的位置。

该标记由以下代码行访问,该代码行位于一个函数内,当我到达它时将显示该函数。

$currency['rate'] = $xml->channel->item[$x]->description;

注意$x我将参数传递到上面的变量。

以下函数位于我的getCurrencyRate.php脚本中。

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

请注意,我已经对这些值进行了硬编码15, 16 and 56 。可以在帖子顶部的第一张图片中查看此输出。我想要做的是从提要中设置的参数中解析这些值,如cityConfig.php脚本中所示。

上面的get_rate函数调用如下:

// 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 />';
}

为了实现我的目标,我get_currency_rate通过添加以下代码行并将数值替换为 variable 来更改上面的函数$x

 $vars = parse_url($feed, PHP_URL_QUERY);
 parse_str($vars);

和修改后的功能:

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

    }

}

上面的输出显示:

在此处输入图像描述

我期望列中的输出与以前相同,但这个不同。有什么想法我哪里出错了吗?

提前致谢

4

1 回答 1

1

get_currency_rate在您的第一个函数中查看您的代码。

    $rate = get_rate($xml, 15); //15欧元
    if ($feed == 'http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16') {
        $rate = get_rate($xml, 16); //16英镑
    } 别的 {
        $rate = get_rate($xml, 56); //56美元
    }

让我们看看它执行了什么。无论是这个,

    $rate = get_rate($xml, 15); //15欧元
    $rate = get_rate($xml, 16); //16英镑

或这个,

    $rate = get_rate($xml, 15); //15欧元
    $rate = get_rate($xml, 56); //56美元

现在。考虑一下您的新get_currency_rate功能将实际执行什么。

    $vars = parse_url($feed, PHP_URL_QUERY);
    parse_str($vars);
    # 这将意味着 $x = 15、16 或其他。这将取决于您的 $feed。
    # 如果你的 $feed **是** http://themoneyconverter.com/rss-feed/USD/rss.xml?x=16
    # 这意味着 $x = 16 并且将执行以下代码。

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

或者,

    $vars = parse_url($feed, PHP_URL_QUERY);
    parse_str($vars);
    # 如果你的 $feed **是** http://themoneyconverter.com/rss-feed/USD/rss.xml?x=15
    # 这意味着 $x = 15 并且将执行以下代码。

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

所以,基本上你正在运行的总是两个相同的 get_rate 调用。

像这样,

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

我现在相信,您可以发现错误。它们都将导致打印出同一行。

    0.76429 欧元
    0.76429 欧元

作为一种解决方案,我建议使用类似于以下的开关盒结构:

    函数 get_currency_rate($feed) {
        $xml = 新的 SimpleXmlElement($feed);
        $vars = parse_url($feed, PHP_URL_QUERY);
        parse_str($vars);
        开关 ($x) {
            案例 15:
                get_rate($xml, 16); //16英镑
                get_rate($xml, 56); //56美元
                休息;
            案例 16:
                get_rate($xml, 15); //15欧元
                get_rate($xml, 56); //56美元
                休息;
            案例 56:默认值:
                get_rate($xml, 15); // 15 欧元
                get_rate($xml, 16); // 16 英镑
                休息;
        }
    }
于 2012-02-06T06:52:19.563 回答