2012-02-19 57 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