<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Custom Live Search - JSON</title>
</head>

<body>

<h1>Custom Live Search - JSON</h1>
	<div style="padding: 10pt; border: medium red solid;">
		<form method="post" action="index.php">	
			<input type="text" id="txtSearch" name="txtSearch" />&nbsp;<input type="submit" id="submit" name="submit" value="Hledat" />
		</form>
	</div>
	<div>
		<?php
			$APPID = "";
			
			if (isset($_POST['submit']) && isset($_POST['txtSearch']))
			{
				echo '<h1>Hledaný výraz: ' . $_POST['txtSearch'] . '</h1>';
				
				$searchTerm = str_replace('+', '%20', urlencode($_POST['txtSearch']));
				$searchUrl = "http://api.search.live.net/json.aspx?AppId=$APPID&Market=cs-CZ&Query=$searchTerm&Sources=web";

				require_once "HTTP/Request.php";
				$req = new HTTP_Request($searchUrl);
				
				$req->sendRequest();
				if($req->getResponseCode() == 200)
				{
					$searchResponse = json_decode($req->getResponseBody());
					
					foreach ($searchResponse->SearchResponse->Web->Results as $webResult)
					{
						echo '<a href="' . $webResult->Url . "'>" . $webResult->Title . '</a>';
						echo '<br />';
					}
				}
			}
		?>
	</div>
</body>

</html>
