<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Search the web</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="shortcut icon" href="/favicon.ico" type="image/ico" />
  <style type="text/css">
   .searchtitle {
     font: 20px sans-serif;
     text-decoration: underline;
   }
   .searchsummary {
     font: 13px sans-serif;
     text-decoration: none;
   }
   .searchurl {
     font: 13px sans-serif;
     text-decoration: none;
   }
  </style>
 </head>
 <body>

<?php
/*
Copyright (c) 2005 Sam J. Clarke
All rights reserved.

Script Name: Yahoo api script
Author Name: Sam J. Clarke
Author Email: admin@free-php.org.uk
Author URI: http://www.free-php.org.uk/
Description: This is a yahoo api search script
Instructions: To use it copy all the code a put it
in a file called index.php and put it on your
webserver. Then just run it, If you need
any help just email me.


LICENSE

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

To read the license please visit http://www.gnu.org/copyleft/gpl.html
*/
$appi "yahooapiid"// your application ID if you dont have one go here http://api.search.yahoo.com/webservices/register_application
$numresults "10"// Specifies number of results must be between 10 and 50
$adult_ok "0"// Specifies whether to allow results with adult content. enter a 1 to allow adult content
$language "en"// The language the results are written in

if(isset($_GET['q']) && !empty($_GET['q'])) {
    if (
get_magic_quotes_gpc()) {
        
$_GET['q'] = stripslashes($_GET['q']);
    }
    
$q urlencode($_GET['q']);
    if(!empty(
$_GET['p']) && is_numeric($_GET['p'])) {
       
$p $_GET['p'];
       
$qp $_GET['p'];
    } else {
       
$qp 1;
       
$p 0;
    }
    
$pagenum $p;
    
$urlquery 'appid='.$appi;
    
$urlquery .= '&&query='.$q;
    
$urlquery .= '&&start='.$qp;
    
$urlquery .= '&&adult_ok='.$adult_ok;
    
$urlquery .= '&&language='.$language;
    
$urlquery .= '&&results='.$numresults;
    
$url "http://api.search.yahoo.com/WebSearchService/V1/webSearch?".$urlquery."";
    
$st_timer microtime(true);
    @
$handle fopen($url"r");
    if(
$handle) {
      
$contents '';
      while (!
feof($handle)) {
         
$contents .= fread($handle8192);
      }
      
fclose($handle);
    }

$p xml_parser_create();
xml_parse_into_struct($p$contents$vals);
xml_parser_free($p);

$title = array();
$summary = array();
$url = array();
$clickurl = array();

// WARNING THE CODE BELOW IS VERY MESSY BECAUSE THIS THE ONLY XML PARSER OUR HOST SUPPORTS

$TOTALRESULTSAVAILABLE $vals[0]['attributes']['TOTALRESULTSAVAILABLE'];

$resultscount 0;
for(
$i=0;isset($vals[$i]);$i++) {
   if(
$vals[$i]['tag'] == 'TITLE') {
       
$title[] = $vals[$i]['value'];
       
$resultscount++;
   } elseif(
$vals[$i]['tag'] == 'SUMMARY') {
       
$summary[] = $vals[$i]['value'];
   } elseif(
$vals[$i]['tag'] == 'URL' && $vals[$i]['level'] == 3) {
       
$url[] = $vals[$i]['value'];
   } elseif(
$vals[$i]['tag'] == 'CLICKURL') {
       
$clickurl[] = $vals[$i]['value'];
   }
}

if (
$resultscount >= $numresults) {
    
$results true;
} else {
    
$results false;
}

print 
"    <form action=\"index.php\" method=\"get\">\n";
print 
"     Search the Web: <input type=\"text\" size=\"40\" name=\"q\" value=\"".$_GET['q']."\" />\n";
print 
"     <input type=\"submit\" value=\"Search\" /><br />\n";
print 
"     Powered by Yahoo! Search\n";
print 
"    </form>\n";

print 
"There were about ".$TOTALRESULTSAVAILABLE." results for ".urldecode($_GET['q'])." (Search took ".round($st_timer,3)." seconds)<br /><br /><br />\n";

$words explode(" ",urldecode($_GET['q']));

for(
$i=0;isset($title[$i]);$i++) {
    
$clickurl[$i] = str_replace("<","&lt;",$clickurl[$i]);
    
$clickurl[$i] = str_replace(">","&gt;",$clickurl[$i]);
    
$title[$i] = str_replace("<","&lt;",$title[$i]);
    
$title[$i] = str_replace(">","&gt;",$title[$i]);
    
$summary[$i] = str_replace("<","&lt;",$summary[$i]);
    
$summary[$i] = str_replace(">","&gt;",$summary[$i]);
    
$url[$i] = str_replace("<","&lt;",$url[$i]);
    
$url[$i] = str_replace(">","&gt;",$url[$i]);
    for(
$x=0;$words[$x];$x++) {
        
$summary[$i] = preg_replace("/\b".preg_quote($words[$x], '/')."\b/is","<b>".$words[$x]."</b>",$summary[$i]);
    }
    print 
"<span class=\"searchtitle\"><a href=\"".$clickurl[$i]."\">".$title[$i]."</a></span><br />\n";
    print 
"<span class=\"searchsummary\">".$summary[$i]."</span><br />\n";
    print 
"<span class=\"searchurl\"><a href=\"".$clickurl[$i]."\">".$url[$i]."</a></span><br /><br /><br />\n";
}

  if(
$pagenum >= $numresults && $results == true) {
    
$prev $pagenum $numresults;
    print 
"<div id=\"searchtitle\"><a href=\"index.php?q=".$q."&amp;&amp;p=".$prev."\">Prev</a>";
    
$next $pagenum $numresults;
    print 
" <a href=\"index.php?q=".$q."&amp;&amp;p=".$next."\">Next</a></div>\n";
  } elseif(
$pagenum $numresults) {
    
$prev $pagenum $numresults;
    print 
"<div id=\"searchtitle\"><a href=\"index.php?q=".$q."&amp;&amp;p=".$prev."\">Prev</a></div>\n";
  } else {
    
$next $pagenum $numresults;
    print 
"<div id=\"searchtitle\"><a href=\"index.php?q=".$q."&amp;&amp;p=".$next."\">Next</a></div>\n";
  }
} else {
  print 
"    <form action=\"index.php\" method=\"get\">\n";
  print 
"     Search the Web: <input type=\"text\" size=\"40\" name=\"q\" />\n";
  print 
"     <input type=\"submit\" value=\"Search\" /><br />\n";
  print 
"     Powered by Yahoo! Search.\n";
  print 
"    </form>\n";
}
?>
 </body>

</html>