我正在從倫敦和紐約的雅虎天氣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
法正常工作。
在此先感謝。
'$ feed = file_get_contents(if(isset($ sourceFeed)){echo $ sourceFeed;});'是一個語法錯誤,並且毫無意義。 – DaveRandom 2012-01-13 16:42:31
@DaveRandom的確。複製/粘貼固定。 – 2012-01-13 16:44:58