[英]Duplicating content with PHP_URL_QUERY with RSS feeds
背景:
我創建了一個動態網站,其中很多內容都來自themoneyconvert.com的RSS源
該網站顯示如下的實時貨幣匯率:
希望您能夠了解我在3列模板中顯示的內容。
到theoneyconverter.com的Feed網址是在我稱之為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');
?>
Feed網址存儲在$currencySource
數組中。 我在每個URL的末尾添加了一個參數。 例如,數組中的第一項將“ ?x=15
添加到現有Feed的末尾。 此參數對應於源URL中<item>
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
進行了硬編碼。可以在帖子頂部的第一張圖片中查看此輸出。 我想要做的是從feed中設置的參數中解析這些值,如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 />';
}
為了實現我的目標,我通過添加以下代碼行並將數值替換為變量$x
來改變上面的get_currency_rate
函數。
$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
}
}
上面的輸出顯示:
我希望列中的輸出與以前相同,但這一個是不同的。 我出錯的任何想法?
提前致謝
在第一個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
作為一個解決方案,我建議一個類似於以下的開關盒結構:
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; } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.