/

  / 

How to Create an XML Data Feed from Your Car Dealership’s Website

This article describes a process for generating an XML feed from vehicle data extracted directly from VDPs on a dealer's website. Of course, the process is not limited to car dealer websites. It can be applied to any type of website. In order to create an XML data feed, you first need to extract the data from your VDPs. After you've extracted the data, you'll now have a JSON object from which to create your XML feed. To convert the JSON to XML, I'm going to use PHP.
<?php
// Decode and sanitize the the Kimono API URL
$url = filter_var(urldecode($_REQUEST['url']),FILTER_SANITIZE_URL);

// If you named the collection in your API something other than collection1 (the default), include the collection parameter in your URL
// e.g. &collection=vdp_data
if(isset($_REQUEST['collection']) && !empty($_REQUEST['collection'])) {
	$collection = filter_var($_REQUEST['collection'],FILTER_SANITIZE_STRING);
} else {
	$collection = 'collection1';
}

// Check that the value of 'url' is a kimono API URL
if(preg_match("~https://www\.kimonolabs\.com/api/[a-z0-9]+\?apikey=[a-zA-Z0-9]+~", $url)) {
	
	// Function definition to convert array to xml (adapted from http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml)
	function array_to_xml($array, &$xml) {
		foreach($array as $key => $value) {
			if(is_array($value)) {
				if(!is_numeric($key)){
					$subnode = $xml->addChild("$key");
					array_to_xml($value, $subnode);
				} else {
					$subnode = $xml->addChild("item");
					array_to_xml($value, $subnode);
				}
			} else {
				$xml->addChild("$key",htmlspecialchars("$value"));
			}
		}
	}
	
	// Initialize session and set URL for cURL.
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	
	// Set so curl_exec returns the result instead of outputting it.
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	    
	// Get the response and close the channel.
	$response = curl_exec($ch);
	
	// Convert the response to an array
	$response = json_decode($response, true);
	
	// Grab just the "collection"
	$response = $response['results'][$collection];
	
	// creating object of SimpleXMLElement
	$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?><inventory></inventory>");
	array_to_xml($response, $xml);
	
	unset($response);
	
	header('content-type: text/xml');
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	$dom = new DOMDocument("1.0");
	$dom->preserveWhiteSpace = false;
	$dom->formatOutput = true;
	$dom->loadXML($xml->asXML());
	echo $dom->saveXML();

} else {
	echo "Oops. Something went wrong.";
}
?>
So, let's say you save this script as a file named feed-generator.php. There are two parameters you can pass to the script.
  • url
    • required
    • the url encoded url of your kimono API
    • e.g. feed-generator.php?url=https%3A%2F%2Fwww.kimonolabs.com%2Fapi%2F54321abc%3Fapikey%3D6543210c7c012d3cae1fbb97bd224466
  • collection
    • optional
    • whatever you named your "collection" when creating your API (not the name of the API, but the collection within the API)
    • e.g. &collection=vdp_data
So, you might end up with a feed URL like: http://dealerwebsite.com/feed-generator.php?url=https%3A%2F%2Fwww.kimonolabs.com%2Fapi%2F54321abc%3Fapikey%3D6543210c7c012d3cae1fbb97bd224466&collection=vdp_data Lo and behold, you've got yourself a XML feed of your VDPs (or whatever data you collected with your Kimono API).