使用 PHP_URL_QUERY 和 RSS 提要复制内容
背景:
我创建了一个动态网站,其中许多内容是由来自 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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看第一个
get_currency_rate
函数中的代码。让我们检查一下它执行了什么。要么这个,
要么这个,
现在。考虑一下新的
get_currency_rate
函数将实际执行什么。或者,
所以,基本上您正在运行的是总是对 get_rate 的两个相同的调用。
就像这样,
我相信现在您可以发现错误。它们都会导致打印出同一行。
作为解决方案,我建议使用类似于以下的 switch-case 结构:
Look at your code in your first
get_currency_rate
function.Let's examine what it does execute. Either this,
or this,
Now. Consider what your new
get_currency_rate
function will actually execute.Or,
So, basically what you are running are allways two identical calls to get_rate.
Like so,
I trust by now, you can spot the error. They will both result in the same line beeing printed out.
As a solution, I would suggest a switch-case construction somewhat like the followng: