我正在設置一個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時,它是從一個函數返回的最佳方式?
查看頁面源代碼,這應該會顯示實際的XML內容。 – ndm
哦哇,有沒有顯示的各種數據!編輯我的問題,以反映如此 – mhopkins321