当我的脚本中发生错误时,我正在生成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