我正在从伦敦和纽约的雅虎天气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