使用 PHP_URL_QUERY 和 RSS 提要复制内容

发布于 2025-01-02 16:46:16 字数 3205 浏览 0 评论 0原文

背景:

我创建了一个动态网站,其中许多内容是由来自 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 添加到现有 Feed 的末尾。此参数对应于 feed 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 和 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

    }

}

上面的输出显示:

在此处输入图像描述

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

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你与清晨阳光 2025-01-09 16:46:16

查看第一个 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;
        }
    }

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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文