[英]Alternative to header(“Content-type: text/xml”);
還有其他東西可以代替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
-刪除空間應該可以解決問題。
您的文字編輯器是什么? 我以前有這個問題,我在“編碼”菜單中使用Notepad ++,在沒有BOM的UTF-8中有一個名為“編碼”的選項,您應該選擇它或在其他編輯器中選擇類似的東西。
您可以通過保存一個空文件並檢查其文件大小來確保。
您可以在此處找到有關BOM的更多信息http://en.wikipedia.org/wiki/Byte_order_mark
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.