Exercice 3

<?php
 
researchForm("saisissez votre requete");
 
control();
 
 
/**
* traitement des parametres
*/
 
function control() {
	if($_SERVER['REQUEST_METHOD']=='POST') {
 		//POST processing
 		$request="http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query='".$_POST["keywords"]."'&results=10";
		$response = file_get_contents($request);
		$dom = new DOMDocument('1.0', 'UTF-8'); 
		if ($dom->loadXML($response) === false) { 
			 die('Parsing failed'); 
		}
		viewResults($dom);	
 	}
 
}
 
/*
* Test xml parsing
*/
function xml_to_result($dom) {
	echo "Affiche la structure<br/>";
	$root = $dom->getElementsByTagName("Result");
	$nb = $root->length;
	foreach($root as $v) {
		$nodes=$v->childNodes ;
		foreach($nodes as $node) {
			echo $node->nodeName."<br/>" ;
		}
		echo "<br/>";
	}
}
 
/*
* Affichage du resulat
*/
function viewResults($dom) {
	echo "Resultat de la recherche<br/>";
	$root = $dom->getElementsByTagName("Result");
	$nb = $root->length;
	echo "<div id=result><div>Il y a $nb resultats pour la recherche</div><br/>" ;
	foreach($root as $v) {
		$nodes=$v->childNodes ;
		$title=utf8_decode($nodes->item(0)->nodeValue);
		$summary=utf8_decode($nodes->item(1)->nodeValue);
		$url=$nodes->item(2)->nodeValue;
		$displayurl=$nodes->item(4)->nodeValue;
		echo <<<EOB
		<div id=result>
			<div id=title><a style='text-decoration:none;' href='{$url}'>{$title}</a></div>
			<div id=summary style='color:grey'>{$summary}</div>
			<div id=displayurl style='color:blue'>{$displayurl}</div>
		</div>
EOB;
		echo "<br/>--------------------------------------<br/>";
	}
}
 
/*
* Formulaire
*/
function researchForm($v) {
	echo <<<EOB
	<form method=post action=index.php>
		<input type=text name=keywords size=100 value='{$v}'/> <INPUT type="submit" value="Envoyer">
	</form>
EOB;
}
 
//echo htmlspecialchars($response, ENT_QUOTES);
 
//echo $response ;
 
?>