2012-02-05 60 views
3

背景:仿型內容與RSS提要PHP_URL_QUERY

我創建其中很多內容是通過RSS生成一個動態的網站從themoneyconvert.com

網站實時顯示貨幣供稿利率像這樣:

enter image description here

希望你得到我DIS思想內容,在3列模板中播放。

供稿網址對themoneyconverter.com都在,我已經叫cityConfig.php

​​

飼料的URL都存儲在$currencySource陣列中的腳本設置。我在每個網址的末尾添加了一個參數。例如,數組中的第一項已添加到現有提要的末尾?x=15。該參數對應於來自供稿網址的<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從這個輸出可以在第一圖像中在所述柱的頂部觀看。我正在嘗試從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 

    } 

} 

從上述顯示器的輸出:

enter image description here

我期待在列中的相同的輸出之前,但這個人是不同。任何想法,我錯了?

在此先感謝

回答

1

看看你的代碼在你的第一個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 

作爲一個解決方案,我建議一個開關殼體結構有點像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; 
     } 
    } 
+1

偉大的答案。 switch語句很有意義。謝謝! – keenProgrammer 2012-02-06 11:21:10