title: Function supposed to return valid xml. Returns 'Extra Content' error instead
body: <p>I am setting up a web API, which I'm sure can be done more efficiently, but this is v0.1. The first step is visiting <code>localhost/serverList/api/rest.php?action=allServers&amp;format=xml</code>. This begins the below chain. I've removed the non relevant parts of the code so that this question is shorter</p>
  
  <p>serverList/api/rest.php</p>
  
  <pre><code>&lt;?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-&gt;flush();
      }
  }
  ?&gt;
  </code></pre>
  
  <p>serverList/api/inc/restFunctions.php</p>
  
  <pre><code>&lt;?php
  function getArrayOfFieldNames($queryResults){
      $fieldList = array();
      while($finfo = $queryResults-&gt;fetch_field()){
          $fieldName = $finfo-&gt;name;
          array_push($fieldList, $fieldName);
      }
      return $fieldList;
  }
  
  function getXMLofQuery($queryResults,$xmlTitle){
      $fieldList = getArrayOfFieldNames($queryResults);
      $xml = new XMLWriter();
      $xml-&gt;openURI("php://output");
      $xml-&gt;startDocument();
      $xml-&gt;setIndent(true);
      $title = $xmlTitle;
      $titlePlural = $xmlTitle."s";
      $xml-&gt;startElement($titlePlural);
      $fieldIDName = $title."ID";
          while($row = $queryResults-&gt;fetch_assoc()){
              $xml-&gt;startElement($title);
                  $xml-&gt;writeAttribute('id', $row[$fieldIDName]);
                  foreach($fieldList as $field){
                          $xml-&gt;startElement($field);
                              $xml-&gt;writeRaw($row[$field]);
                          $xml-&gt;endElement();
                      }
              $xml-&gt;endElement();
          }
      $xml-&gt;endElement();
      return $xml;
  }
  function allserversxml(){
      global $link; //from config.php file
      $allServerResults = $link-&gt;query("SELECT * FROM servers");
      $xml = getXMLofQuery($allServerResults,"server");
      return $xml;
  }
  ?&gt;
  </code></pre>
  
  <p>The problem is that when I go to the URL, I get the error <code>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.</code></p>
  
  <p>Yet...below there is no rendering. What gives?</p>
  
  <p>EDIT: as per ndm's recommendation, I was able to get the error via the source of the page.</p>
  
  <pre><code>Call to a member function flush() on a non-object in C:\path\serverList\api\rest.php on line 29
  </code></pre>
  
  <p>So I suppose my question would be then, what is the best way to display xml on a page when it is returned from a function?</p>