2013-03-05 52 views
0

以下是錯誤我得到的, 此頁面包含以下錯誤:第1欄第1行Web服務XML編碼錯誤

錯誤:在文檔 誤差在1號線末額外內容第1列:編碼錯誤

下面是頁面渲染到第一個錯誤。

以下是代碼,

<?php 

if (isset($_GET['itemno'])) { 
    $number_of_posts = isset($_GET['number']) ? intval($_GET['number']) : 20; 
    $format   = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; 
    $user_id   = intval($_GET['itemno']); 
    /* connect to mysql database */ 
    $link = mysql_connect('localhost', 'root', '') or die('Can not connect to the Database'); 
    mysql_select_db('test', $link) or die('Can not select the Database'); 
    /* select records from the database */ 
    $query = "SELECT qty, price FROM postatusinfo WHERE itemno = $user_id"; 
    $result = mysql_query($query, $link) or die('Errant query: ' . $query); 
    /* create array of the records */ 
    $posts = array(); 
    if (mysql_num_rows($result)) { 
     while ($post = mysql_fetch_assoc($result)) { 
      $posts[] = array('post' => $post); 
     } 
    } 
    /* output in required format */ 
    if ($format == 'json') { 
     header('Content-type: application/json'); 
     echo json_encode(array('posts' => $posts)); 
    } else { 
     header('Content-type: text/xml'); 
     echo '<posts>'; 
     foreach ($posts as $index => $post) { 
      if (is_array($post)) { 
       foreach ($post as $key => $value) { 
        echo '<', $key, '>'; 
        if (is_array($value)) { 
         foreach ($value as $tag => $val) { 
          echo '<', $tag, '>', htmlentities($val), '</', $tag, '>'; 
         } 
        } 
        echo '</', $key, '>'; 
       } 
      } 
     } 
     echo '</posts>'; 
    } 
    /* close database connection */ 
    @mysql_close($link); 
} 
+0

沒有看到文本輸出本身,就不可能知道這裏發生了什麼。 (*更早的編輯:*您在問題中使用的PHP代碼無效,我修正了它,請在您發佈代碼之前仔細檢查,還要正確縮進。)此錯誤與XML無關,至少與JSON無關 - 至少據我所知這些錯誤信息。 – hakre 2013-03-05 08:38:40

+0

對不起。雅我也發現XML中的錯誤 – 2013-03-05 08:55:01

+0

你應該留下一些評論形式的信息,然後是什麼導致你的問題,所以如果別人發現你的問題在這裏至少有一個提示什麼幫助你。這不是一個真正的答案,但分享一個關於你如何解決它的評論並沒有傷害。 – hakre 2013-03-05 09:18:25

回答

0

我覺得這是有文件的編碼問題,特別是與BOM

嘗試用顯示BOM(如記事本++)的編輯器打開,然後嘗試刪除它。