我正在尝试输出线路中电话的当前时间和温度。它需要作为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.' '.$temp.' °'.$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"> </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中,有什么建议吗?
输出文本后不能发送头端。将所有打印和回显实例放在最后一个标头调用之后。
如果需要表单数据,可以使用重定向。
我最终让它与以下一起工作
<?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>";
?>