2012-01-13 158 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; 

}