'; /** * A little 'controller' to make this sample page work: * */ if(isset($_GET['p']) && $_GET['p'] != '') { $url = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?'; $params = setParams($id, $site, $numresults); $url = buildUrl($url, $params); $data = getXMLArray($url); $xhtml = $form.formatYahooResultset($data); } else { // If an empty search was given: $xhtml = (isset($_GET['p']) && $_GET['p'] == '') ? '

Please enter a search term

':''; // Show the form: $xhtml .= $form; } echo $xhtml; // // // Here be functions.... // // /** * Build an array with the parameters we want to use. * */ function setParams($id, $site, $numresults) { $params = array( 'appid' => $id, 'site' => $site, 'query' => urlencode($_GET['p']), 'results' => $numresults ); return $params; } /** * A small url building function: * */ function buildUrl($url, $params) { $qstring = ''; // Initialize... foreach($params as $key => $value) { // Build query string... $qstring .= '&'."$key=$value"; } $qstring = ltrim($qstring,'&'); // Trim the fat... return $url.$qstring; } /** * This function accesses an XML file ($url), reads it into a variable, * and then unserializes it into an associative array: */ function getXMLArray($url) { $xml = ''; // Access the file ~ make the request: $handle = fopen($url,"rb"); while (!feof($handle)) { // Read the resulting XML into the variabl $xml: $xml .= fread($handle, 8192); } fclose($handle); // Include the library: include('xml.php'); // Unserialize the data: $data = XML_unserialize($xml); return $data; } /** * This function formats our Yahoo! specific array: * */ function formatYahooResultset($data) { // If no results were found: if($data['ResultSet attr']['totalResultsReturned'] == 0) { $results = '

There were 0 results found with Yahoo! search. Please try again by typing your search terms into the search box, and then click search.

'; return $results; } // Now down to business: $tmp = ''; foreach($data['ResultSet']['Result'] as $key => $value) { $tmp .= '
'.$data['ResultSet']['Result'][$key]['Title'].'
'; $r = htmlentities($data['ResultSet']['Result'][$key]['Summary']); $tmp .= '
'.$r.'
'; } $results = '

These '.$data['ResultSet attr']['totalResultsReturned'].' results provided by Yahoo! search.

'.$tmp.'
'; return $results; } ?>