Location: PHPKode > scripts > SearchGooSimple PHP Google API Search > searchgoosimple-php-google-api-search/searchgoosimple.php
<?php 
// searchgoosimple.php v1.1 - Search script using the Google API 
// Copyright (C) 2004  soft-spot.co.uk

// You are free to use or modify this script provided  you  keep the "Powered by" line 
// as an active hyperlink on all the pages generated by this script.
//This software 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.

// Edit the following line with your Google API Key:

$APIkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Set to your Google API Key

// Edit the following parameters (optional):
$sitesearch = ""; // Set to default site for site search or leave blank for the entire web.
$siteentry = false; // Set to true to enable entry of site in search form, false to disable entry of site
$apiurl = "http://api.google.com/search/beta2";
$maxresults = 10; // Google API allows maximum of 10

// Do not edit below here  

$self = $_SERVER["PHP_SELF"];
$query = getvars("query", "");
$site  = getvars("site", "$sitesearch");

?>    
<!-- SEARCH FORM -->
<div align="left">
  <form method="GET" action="<?php echo $self; ?>">
  <table border="0" cellspacing="0" id="table1" cellpadding="0">
    <tr>
      <td align="right">
      <font face="Arial" size=2>Search for: </font>
      </td>
      <td>
      <font face="Arial">
        <input type="text" name="query" size="55" value="<?php echo stripslashes(htmlspecialchars($query, ENT_QUOTES)); ?>"> <input type="submit" value="Search" name="action">
        </font>
      </td>
    </tr>
 <?php
if($siteentry) {
?>
    <tr>      
       <td align="right">
       <font face="Arial" size=2>in site: </font>
      </td>
      <td>
      <font face="Arial" size=2>
        <input type="text" name="site" size="30" value="<?php echo $site; ?>">
        (Leave blank to search entire web)
        </font>
      </td>
    </tr>
 <?php
}
?>
  </table>
  </form>
</div>  
<!-- END OF SEARCH FORM -->

<?php 
include_once('nusoap.php');
  
if ($query != "") {

$fullquery = $query;
if ($site != "") {$fullquery .= " site:$site";}

$start = intval(getvars("start", 0));
    
$APIparams = array("key"         => $APIkey,
                   "q"           => $fullquery,                 
                   "start"       => $start,
                   "maxResults"  => $maxresults,
                   "filter"      => true,
                   "restrict"    => "",
                   "safeSearch"  => true,
                   "lr"          => "lang_en",
                   "ie"          => "",
                   "oe"          => "");      
  
// Create a new soap client 
$Client = new soapclient($apiurl);
   
$QueryResult = $Client->call("doGoogleSearch", $APIparams, "urn:GoogleSearch");
$total       = $QueryResult["estimatedTotalResultsCount"]; 
if ($total > 0)
{ 
    echo '<hr><font face="Arial" size="3">Search results:<br><br></font>';
     
    $QueryResult = $QueryResult["resultElements"]; 

    for ($i = 0; $i < count($QueryResult); $i++)
    { 
        $element     = $QueryResult[$i];
        $snippet[$i] = utf8_decode($element["snippet"]);
        $url[$i]     = $element["URL"];
        $title[$i]   = utf8_decode($element["title"]);
        if ($title[$i] == "") {$title[$i] = str_replace("http://","",$url[$i]);}
        echo '<p class="result"><font face="Arial" size="3"><a href="' . $url[$i] .           '">' . $title[$i] . '</a></font><br>';
        if ($snippet[$i] != "") {
            echo '<font face="Arial" size="2">' . $snippet[$i] . '</font><br>';
            }
        echo '<font face="Arial" size="1">' . $url[$i] . '</font></p>';
    }
}
else
{ 
    echo "Sorry, no search results found";
}
}
echo '<br><p><font size="1" face="Arial">Search Results are from Google and are subject to Google\'s terms of use';    
echo '<p><font size="1" face="Arial">Powered by <a href="http://www.soft-spot.co.uk">Soft-Spot.co.uk</a></font></p>';

//Function to get variable from POST, GET, cookie or use default
function getvars($var, $default)
{
    //Get from POST
    if(isset($_POST[$var]))
    {
      $var = rawurldecode($_POST[$var]);
    }
    else
    {
      //Get from GET
      if(isset($_GET[$var]))
      {
        $var = rawurldecode($_GET[$var]);
      }
      else
      {
        //Get from a cookie if one exists
        if (isset($_COOKIE[strtoupper($var)]))
        {
          $var = rawurldecode($_COOKIE[strtoupper($var)]);
        }
        else
        {
          //set to default if not found
          $var = $default;
        }
      }
    }
    return stripslashes($var);
}

?>
Return current item: SearchGooSimple PHP Google API Search