2013-07-09 40 views
-2

我正在设置一个Web API,我相信这可以更有效地完成,但这是v0.1。第一步是访问localhost/serverList/api/rest.php?action=allServers&format=xml。这从下面的链开始。我已经删除了代码的非相关部分,使这个问题更短函数应该返回有效的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时,它是从一个函数返回的最佳方式?

+0

查看页面源代码,这应该会显示实际的XML内容。 – ndm

+0

哦哇,有没有显示的各种数据!编辑我的问题,以反映如此 – mhopkins321

回答

1

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

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

哦,我的天啊。我感觉超级粗心。非常感谢 – mhopkins321

+0

不客气,大家都知道,感受:) – ndm