當我的腳本中發生錯誤時,我正在生成XML響應,但是XML響應包含錯誤「僅在文檔開始處允許XML聲明」。從我所做的研究中,我收集到這是由開放XML聲明之前的空白引起的。 (這正是我的XML響應所具有的。)XML文檔開始處的空白造成錯誤
<?xml version="1.0" charset="utf-8"?>
<conv>
<error code="1000">Required parameter is missing</error>
</conv>
我不能找到此問題的來源?這裏是產生響應的代碼:
function result($data,$errnum,$type='XML') {
switch(strtolower($type)) {
case 'xml':
// Build the XML body
header('Content-Type: text/xml');
$xmlpage = "<?xml version=\"1.0\" charset=\"utf-8\"?>\n";
$xmlpage .= "<conv>\n";
$xmlpage .= "\t<error code=\"$errnum\">$data</error>\n";
$xmlpage .= '</conv>';
echo $xmlpage;
break;
}
exit;
}
任何想法?
編輯 -
我通過一個空白移除工具運行的頁面,它消除在開始的空白,但現在我得到一個新的錯誤:
<?php
function error($errnum=1000) {
$data = array(
'1000' => 'Required parameter is missing',
'1100' => 'Parameter not recognized',
'2000' => 'Currency type not recognized',
'2100' => 'Currency amount must be to 2 decimal places',
'2200' => 'Currencies cannot be the same',
'3000' => 'Service currently unavailable',
'3100' => 'Error in service'
);
result($data[$errnum], $errnum);
}
function result($data,$errnum,$type='XML') {
switch(strtolower($type)) {
case 'xml':
// Build the XML body
header('Content-Type: text/xml');
$xmlpage = "<?xml version=\"1.0\" charset=\"utf-8\"?>\n";
$xmlpage .= "<conv>\n";
$xmlpage .= "\t<error code=\"$errnum\">$data</error>\n";
$xmlpage .= '</conv>';
echo $xmlpage;
break;
}
exit;
}
?>
錯誤:錯誤在第1行第19列:解析XML聲明:'?>'預計
編輯2:刪除charset = \「utf-8 \修復它!但我需要那裏,所以我很困惑,爲什麼這是造成問題?
腳本中是否有任何空格?在<?php'之前? – thetaiko 2012-02-19 16:04:22
打開'<?php'標籤之前'.php'文件中沒有空格/換行符? – jensgram 2012-02-19 16:04:24
在您的腳本使用的任何文件中的任何標記之前/之後可能會有空格。您可以在PHP文件末尾省略任何?>標記 – Vitamin 2012-02-19 16:06:13