【问题标题】:Function supposed to return valid xml. Returns 'Extra Content' error instead [closed]函数应该返回有效的 xml。而是返回“额外内容”错误[关闭]
【发布时间】:2013-07-09 21:08:15
【问题描述】:

我正在设置一个 Web API,我确信可以更有效地完成,但这是 v0.1。第一步是访问localhost/serverList/api/rest.php?action=allServers&format=xml。这开始了下面的链。我已经删除了代码的不相关部分,以便这个问题更短

serverList/api/rest.php

<?php
include 'inc/restFunctions.php';
//several lines of code removed. $functionName = allserversxml
if(in_array($action,$possibleActions)){
    if(in_array($format,$possibleFormats)){
        $functionName = $action.$format;
        $result = $functionName();
        header('Content-type: text/xml');
        $return->flush();
    }
}
?>

serverList/api/inc/restFunctions.php

<?php
function getArrayOfFieldNames($queryResults){
    $fieldList = array();
    while($finfo = $queryResults->fetch_field()){
        $fieldName = $finfo->name;
        array_push($fieldList, $fieldName);
    }
    return $fieldList;
}

function getXMLofQuery($queryResults,$xmlTitle){
    $fieldList = getArrayOfFieldNames($queryResults);
    $xml = new XMLWriter();
    $xml->openURI("php://output");
    $xml->startDocument();
    $xml->setIndent(true);
    $title = $xmlTitle;
    $titlePlural = $xmlTitle."s";
    $xml->startElement($titlePlural);
    $fieldIDName = $title."ID";
        while($row = $queryResults->fetch_assoc()){
            $xml->startElement($title);
                $xml->writeAttribute('id', $row[$fieldIDName]);
                foreach($fieldList as $field){
                        $xml->startElement($field);
                            $xml->writeRaw($row[$field]);
                        $xml->endElement();
                    }
            $xml->endElement();
        }
    $xml->endElement();
    return $xml;
}
function allserversxml(){
    global $link; //from config.php file
    $allServerResults = $link->query("SELECT * FROM servers");
    $xml = getXMLofQuery($allServerResults,"server");
    return $xml;
}
?>

问题是当我转到 URL 时,我收到错误 error on line 2 at column 1: Extra content at the end of the document. Below is a rendering of the page up to the first error.

然而……下面没有渲染。什么给了?

编辑:根据 ndm 的建议,我能够通过页面的来源得到错误。

Call to a member function flush() on a non-object in C:\path\serverList\api\rest.php on line 29

所以我想我的问题是,当从函数返回时,在页面上显示 xml 的最佳方式是什么?

【问题讨论】:

  • 查看页面源代码,这应该会显示实际的 XML 内容。
  • 哦哇哦,这里有各种各样的数据没有显示出来!编辑我的问题以反映这一点

标签: php xml function mysqli


【解决方案1】:

据我从错误消息和代码中可以看出,假设“删除不相关的代码部分”不包括从发布的函数和逻辑流程中删除代码,它看起来像您要调用 flush() 的变量应该是 $result 而不是 $return

...
$result = $functionName();
header('Content-type: text/xml');
$result->flush(); // like this

【讨论】:

  • 天哪。我觉得超级粗心。非常感谢
  • 不客气,我们都知道那种感觉 :)
猜你喜欢
  • 2022-11-03
  • 2020-06-29
  • 2019-04-27
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 2021-07-17
  • 2022-01-12
  • 1970-01-01
相关资源
最近更新 更多