Generating XML feeds
In the last section, we built our API to fetch the data and print the raw data onto the page. In this section, we will build methods that will take the data and convert them into XML feeds. A remote application can then use these XML feeds to ingest the data. Before we build the XML generation functionality, let's create a class variable that can be used to hold this XML data using the following code in the controllers/api.php
file:
public $xml;
Now that we have added the class variable, let's add the following XML generation functionality to our API, present in the controllers/api.php
file:
private function _generateXML($root, $data){ $this->xml = new SimpleXMLElement("<$root/>"); foreach($data as $key=>$value){ $this->_generateXMLChild(substr($root, 0, -1), $value); } header("HTTP/1.1 200 OK"); header("Content-Type: application/xml; charset=utf-8"); echo $this->xml->asXML(); } private function _generateXMLChild($type ,$item){ ...