2014-08-31 88 views
1

我正在嘗試輸出當前時間和電話的溫度。它需要以XML格式輸出。到目前爲止,我有帶XML的PHP​​ XML輸出?

<?php 
header("content-type: text/xml"); 
echo "<?xml version='1.0' encoding='UTF-8'?>"; 
echo "<Response>"; 
echo "<Say>"; 
echo('Time is'); 
echo date('H:i'); 
echo('Temp is'); 
echo "</Say>"; 
echo "</Response>"; 
?> 

我遇到的問題是越來越溫度。我有我一直在嘗試使用的以下示例。但每次我試圖把它列入我得到警告:不能更改頭信息 - 已經

<?php 

function getWeatherRSS($weatherLink){ 

    if ($fp = fopen($weatherLink, 'r')) { 
     $content = ''; 

     while ($line = fread($fp, 1024)) { 
     $content .= $line; 
     } 
    } 

    return $content; 
} 

function processWeather($wurl){ 

    $wrss = getWeatherRSS($wurl); 
    $temp = '-'; 
    $tempu = ''; 
    $city = ''; 
    if (strlen($wrss)>100){ 
     // Get temperature unit C or F 
     $spos = strpos($wrss,'yweather:units temperature="')+strlen('yweather:units temperature="'); 
     $epos = strpos($wrss,'"',$spos); 
     if ($epos>$spos){ 
      $tempu = substr($wrss,$spos,$epos-$spos); 
     } 

     $spos = strpos($wrss,'yweather:wind chill="')+strlen('yweather:wind chill="'); 
     $epos = strpos($wrss,'"',$spos); 
     if ($epos>$spos){ 
      $temp += substr($wrss,$spos,$epos-$spos); 
     } else { 
      $temp = '-'; 
     } 

     // Get city name 
     $spos = strpos($wrss,'yweather:location city="')+strlen('yweather:location city="'); 
     $epos = strpos($wrss,'"',$spos); 
     if ($epos>$spos){ 
      $city = substr($wrss,$spos,$epos-$spos); 
     } 

    } 

    return $city.' &nbsp;'.$temp.' &deg;'.$tempu; 

} 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>Micro Weather</title> 
    <link href="style/style.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
    <div id="main"> 
     <div id="caption">CURRENT WEATHER</div> 
     <div id="icon2">&nbsp;</div> 
     <div id="result"><?php echo processWeather('http://xml.weather.yahoo.com/forecastrss?p=USAR0543&u=f'); ?> 
     </div> 
     <div id="source">Micro Weather 1.0</div> 
    </div> 
</body> 

我如何能的溫度傳遞到XML的任何提示發送了頭?

+0

工作修復頭部差錯。 http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – 2014-08-31 23:13:17

回答

0

輸出文本後無法發送headerd。在最後的標題調用之後放置所有打印和回顯實例。

如果需要,可以使用表單數據進行重定向。

0

我最終得到它具有以下

<?php 
date_default_timezone_set('America/Chicago'); 


function getWeatherRSS($weatherLink){ 

    if ($fp = fopen($weatherLink, 'r')) { 
     $content = ''; 

     while ($line = fread($fp, 1024)) { 
     $content .= $line; 
     } 
    } 

    return $content; 
} 

function processWeather($wurl){ 

    $wrss = getWeatherRSS($wurl); 
    $temp = '-'; 
    $tempu = ''; 
    $city = ''; 
    if (strlen($wrss)>100){ 
     // Get temperature unit C or F 
     $spos = strpos($wrss,'yweather:units temperature="')+strlen('yweather:units temperature="'); 
     $epos = strpos($wrss,'"',$spos); 
     if ($epos>$spos){ 
      $tempu = substr($wrss,$spos,$epos-$spos); 
     } 

     $spos = strpos($wrss,'yweather:wind chill="')+strlen('yweather:wind chill="'); 
     $epos = strpos($wrss,'"',$spos); 
     if ($epos>$spos){ 
      $temp += substr($wrss,$spos,$epos-$spos); 
     } else { 
      $temp = '-'; 
     } 

     // Get city name 
     $spos = strpos($wrss,'yweather:location city="')+strlen('yweather:location city="'); 
     $epos = strpos($wrss,'"',$spos); 
     if ($epos>$spos){ 
      $city = substr($wrss,$spos,$epos-$spos); 
     } 

    } 

    return $temp; 


} 
    $a = processWeather('http://xml.weather.yahoo.com/forecastrss?p=USAR0543&u=f'); 

header("content-type: text/xml"); 
echo "<?xml version='1.0' encoding='UTF-8'?>"; 
echo "<Response>"; 
echo "<Say>"; 
echo('TXK Today Time is'); 
echo "</Say>"; 
echo "<Say>"; 
echo date('g:i '); 
echo "</Say>"; 
echo "<Say>"; 
echo('Current Temperature'); 
echo "</Say>"; 
echo "<Say>"; 
echo $a; 
echo "</Say>"; 
echo "</Response>"; 
?>