douchun6108 2012-01-13 16:24
浏览 39
已采纳

重用PHP函数来减少代码重复

I am pulling in weather data from a Yahoo weather RSS feed for both London and New York. I'm trying to reduce code duplication by reusing a PHP file which contains functions for pulling in the weather data.

Below is the function I am calling - get_current_weather_data(). This function goes off to various other functions such at the get_city() and get_temperature() functions.

<?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;
}

In my index.php I set the URL for the RSS feed as the variable called $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';

?>

I attempt to call the feed in my get_current_weather_data() function using:

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

However, I receive the following error

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

If I replace

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

with the URL for the feed it works but this will stop me from reusing the code. Am I trying to do the impossible or is my syntax just incorrect?

This isset method works fine where used elsewhere like for example the $tabTitle and $pageIntroductionHeading variables just need for the RSS feed.

Thanks in advance.

  • 写回答

6条回答 默认 最新

  • dongma7796 2012-01-13 16:29
    关注

    The problem is in the following line:

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

    it has to be:

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

    And when you call the function, you also have to pass $sourceFeed as function parameter, like that:

    get_current_weather_data($sourceFeed);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。