还有别的东西可以代替header("Content-type: text/xml");
我想从我的数据库中生成XML,但不断收到以下错误:
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/php32/Eindopdracht/api.php:1) in /Applications/MAMP/htdocs/php32/Eindopdracht/core/functions/api.php on line 41
我已经检查过其中有任何空格,但我认为这不会导致错误。这是我用PHP编写的函数:
function generateXML(){
$sql="SELECT * FROM csvupload";
$result=mysql_query($sql);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('chords');
while ($row = mysql_fetch_assoc($result)) {
$xml->startElement("chord");
$xml->writeAttribute('E', $row['item1']);
$xml->writeAttribute('a', $row['item2']);
$xml->writeAttribute('d', $row['item3']);
$xml->writeAttribute('g', $row['item4']);
$xml->endElement();
}
$xml->endElement();
header('Content-type: text/xml');
$xml->flush();
}
您的header
调用必须在任何内容输出到浏览器之前发生。
很有可能,文件的第一行类似于[space]<?php
- 删除空格应该可以解决问题。
你的文本编辑器是什么? 我以前遇到过这个问题,我在编码菜单中使用记事本++,有一个名为 Encode 的选项,UTF-8 没有 BOM,您应该选择它或其他编辑器中的类似内容。
您可以通过保存空文件并检查其文件大小来确保。
您可以在此处找到有关物料清单的更多信息 http://en.wikipedia.org/wiki/Byte_order_mark