php - 使用带有 RSS 提要的 PHP_URL_QUERY 复制内容

标签 php xml rss parse-url

背景:

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

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

enter image description here

希望您了解我在 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通过添加以下代码行并将数值替换为变量 $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

    }

}

以上显示的输出:

enter image description here

我希望列中的输出与以前相同,但这一列不同。我哪里出错了?

提前致谢

最佳答案

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

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

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

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

或者这个,

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

现在。考虑您的新 get_currency_rate 函数将实际执行什么。

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

或者,

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

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

像这样,

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

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

    0.76429 EUR
    0.76429 EUR

作为解决方案,我建议使用类似于以下的 switch-case 结构:

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

关于php - 使用带有 RSS 提要的 PHP_URL_QUERY 复制内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9153656/

相关文章:

java - 特殊情况下的xml文件查询

go - 如何开发 RSS Feeder

java - 区分 RSS 描述元素中的 HTML/XHTML 和纯文本

javascript - Firefox 和 IE 之间 GeoRSS 叠加的加载时间不一致

php - 使用正则表达式删除相同类型的 html 嵌套标签的最终解决方案?

javascript - xmlHTTPrequest 不会打开 ("GET", url, true);我很生气! PHP

php - UTF-8 字符集问题

php - 如何使用 CDbCriteria 编写查询来计算 yii 中的外键?

java - 使用 cyberneko 解析 html 以查找 'div' -标签

java - 将xml文档导入到jaxb创建的实体结构中