<?php

$terms = "";

define("AWS_ACCESS_KEY", "amazon_key");

if(!empty($_GET['terms']))
{
	$terms = $_GET['terms'];
	
	$request = 'http://ecs.amazonaws.com/onca/xml' . 
		'?Service=AWSECommerceService&' . 
		'AWSAccessKeyId=' . AWS_ACCESS_KEY .
		'&Operation=ItemSearch' . 
		'&SearchIndex=Books' . 
		'&ResponseGroup=Medium' . 
		'&Keywords=' . $terms;
	
	$rawXml = file_get_contents($request);
	$xmlResponse = simplexml_load_string($rawXml);
	
	$nodeCount = 4;
	
	$xml = "<?xml version=\"1.0\" ?>\n<books>";
	
	for($i=0; $i < $nodeCount; $i++)
	{
		$xml .= "	<book>";
		$xml .= "		<title><![CDATA[" . $xmlResponse->Items->Item[$i]->ItemAttributes->Title . "]]></title>";
		$xml .= "		<author><![CDATA[" . $xmlResponse->Items->Item[$i]->ItemAttributes->Author . "]]></author>";
		$xml .= "		<price><![CDATA[" . $xmlResponse->Items->Item[$i]->ItemAttributes->ListPrice->FormattedPrice . "]]></price>";
		$xml .= "		<thumb>" . $xmlResponse->Items->Item[$i]->SmallImage->URL . "</thumb>";
		$xml .= "	</book>";
	}
	
	$xml .= "</books>";
	
	header("content-type: text/xml");
	
	print $rawXml;
	
}

?>