web服务XML编码错误


webservice XML encoding error

下面是我得到的错误,本页包含以下错误:

第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);
}

我认为这是文件编码的问题,特别是BOM。

尝试打开显示BOM的编辑器(如notepad++),然后尝试删除它。