2012-02-19 74 views
0

当我的脚本中发生错误时,我正在生成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 \修复它!但我需要那里,所以我很困惑,为什么这是造成问题?

+0

脚本中是否有任何空格?在<?php'之前? – thetaiko 2012-02-19 16:04:22

+0

打开'<?php'标签之前'.php'文件中没有空格/换行符? – jensgram 2012-02-19 16:04:24

+0

在您的脚本使用的任何文件中的任何标记之前/之后可能会有空格。您可以在PHP文件末尾省略任何?>标记 – Vitamin 2012-02-19 16:06:13

回答

0

解决了!

我用这个工具来删除空格: http://www.design215.com/toolbox/whitespace.php

我也有“字符集”在我的XML声明中设置好它应该是“编码”。 EG:

<?xml version="1.0" encoding="utf-8"?> 
<conv> 
    <error code="1000">Required parameter is missing</error> 
</conv> 
0

检查文件编码;确保你使用的是UTF-8,它没有BOM。

+0

更改编码为UTF8,但它并没有帮助我害怕。 – tctc91 2012-02-19 16:21:02

+0

尝试使用输出缓冲(http://www.php.net/manual/ru/ref.outcontrol.php) – Electronick 2012-02-19 16:27:26

+0

尝试,但没有运气仍然:(检查我的OP,因为我设法删除空白,但我现在有一个新的错误。 – tctc91 2012-02-19 16:29:54