2012-01-13 178 views
1

我正在從倫敦和紐約的雅虎天氣RSS提要中獲取天氣數據。我試圖通過重複使用包含函數來提取天氣數據的PHP文件來減少代碼重複。重複使用PHP函數以減少重複代碼

以下是我打來的功能 - get_current_weather_data()。此功能可用於其他各種功能,如get_city()get_temperature()功能。

<?php 

function get_current_weather_data() { 

// Get XML data from source 

include_once 'index.php'; 
$feed = file_get_contents(if (isset($sourceFeed)) { echo $sourceFeed; }); 

// Check to ensure the feed exists 
if (!$feed) { 
    die('Weather not found! Check feed URL'); 
} 
$xml = new SimpleXmlElement($feed); 

$weather = get_city($xml); 
$weather = get_temperature($xml); 
$weather = get_conditions($xml); 
$weather = get_icon($xml); 

return $weather; 
} 

在我index.php我設置的URL RSS Feed作爲所謂$sourceFeed變量。

<?php 

$tabTitle = ' | Home'; 
$pageIntroductionHeading = 'Welcome to our site'; 
$pageIntroductionContent = 'Twinz is a website which has been created to bring towns together! 
      Our goal is to bring communities around the world together, by providing 
      information about your home town and its twin town around the world. Our 
      site provides weather, news and background information for London and 
      one of its twin cities New York.'; 
$column1Heading = 'Current Weather for New York'; 
$column2Heading = 'Current Weather for London'; 
$column3Heading = 'Current Weather for Paris'; 

$sourceFeed = "http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f"; 

include_once 'header.php'; 
include_once 'navigationMenu.php'; 
include_once 'threeColumnContainer.php'; 
include_once 'footer.php'; 

?> 

我嘗試使用調用飼料在我get_current_weather_data()功能:

(if (isset($sourceFeed)) { echo $sourceFeed; }). 

不過,我收到以下錯誤

"Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in C:\xampp\htdocs\Twinz2\nyWeather.php on line 10 
Weather not found! Check feed URL". 

如果我更換

(if (isset($sourceFeed)) { echo $sourceFeed; }) 

與U RL的飼料它的工作原理,但這將阻止我重新使用代碼。我想要做不可能的事情嗎?還是我的語法錯誤?其中,像其他地方例如$tabTitle$pageIntroductionHeading變量只需要一個RSS源使用

isset法正常工作。

在此先感謝。

回答

1

問題出在下面一行:

$feed = file_get_contents(if (isset($sourceFeed)) { echo $sourceFeed; }); 

它必須是:

if(isset($sourceFeed)){ $feed = file_get_contents($sourceFeed); } 

當你調用函數,你也必須通過$sourceFeed作爲函數參數,如下:

get_current_weather_data($sourceFeed); 
2

您正試圖在函數內部訪問全局變量$sourceFeed。把它作爲參數傳遞給函數:

// Pass $sourceFeed as a function parameter: 
function get_current_weather_data($sourceFeed) { 

    // Get XML data from source 

    include_once 'index.php'; 
    $feed = file_get_contents($sourceFeed)); 

    // Check to ensure the feed exists 
    if (!$feed) { 
     die('Weather not found! Check feed URL'); 
    } 
    $xml = new SimpleXmlElement($feed); 

    $weather = get_city($xml); 
    $weather = get_temperature($xml); 
    $weather = get_conditions($xml); 
    $weather = get_icon($xml); 

    return $weather; 
} 

,並調用函數爲:

$sourceFeed = "http://weather.yahooapis.com/forecastrss?p=USNY0996&u=f"; 
$weather = get_current_weather_data($sourceFeed); 
+0

'$ feed = file_get_contents(if(isset($ sourceFeed)){echo $ sourceFeed;});'是一個語法錯誤,並且毫無意義。 – DaveRandom 2012-01-13 16:42:31

+0

@DaveRandom的確。複製/粘貼固定。 – 2012-01-13 16:44:58

1

儘量讓$ sourceFeed全球這樣的:

function get_current_weather_data() { 
    global $sourceFeed 

OR

get_current_weather_data($sourceFeed) 
1

您的問題我是一個可變範圍問題。進入函數內部的變量是一個僅用於該函數的新變量。一旦函數返回/完成,它將無法訪問。所以,你需要讓函數知道什麼值:

function get_current_weather_data($sourceFeed) { // this tells the function to 
// read that variable when it starts. You also need to pass it when you call the function. 

get_current_weather_data($sourceFeed); 

// OR 
get_current_weather_data('http://myurl.com'); 
0

我相當肯定,你在那裏做的甚至不會解析。它當然不會在PHP 5.2中解析。

$feed = file_get_contents(if (isset($sourceFeed)) { echo $sourceFeed; }); 

這是無效的。您不能將一個if語句放在對函數的調用中,並且即使這樣做不起作用,也不會影響該函數的調用方式。

您可以使用三元表達式:

$feed = file_get_contents((isset($sourceFeed)) ? $sourceFeed : ''); 

......但即使這樣也不能幫助你在這裏,因爲你有一個文件名傳遞給file_get_contents()或者你會得到你看到的錯誤。

你的做法是錯誤的,而不是包括index.php來定義你的變量,你應該把變量作爲參數傳遞給函數。例如:

function get_current_weather_data($sourceFeed) { 

    // Get XML data from source 

    $feed = file_get_contents($sourceFeed); 

    // Check to ensure the feed exists 
    if (!$feed) { 
     die('Weather not found! Check feed URL'); 
    } 
    $xml = new SimpleXmlElement($feed); 

    $weather = get_city($xml); 
    $weather = get_temperature($xml); 
    $weather = get_conditions($xml); 
    $weather = get_icon($xml); 

    return $weather; 

}