2014-08-31 4 views
1

전화의 현재 시간과 온도를 한 줄로 출력하려고합니다. 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 –

답변

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