<?php

/*
 *
 *    replace "Your Google License KEY" below with Your Google License KEY
 *
 *    Get NuSOAP from http://sourceforge.net/projects/nusoap/
 *
*/

require_once("nusoap.php");

function 
GoogleSpellCheck()
{
        
// create a instance of the SOAP client object
        
$soapclient = new soapclient("http://api.google.com/search/beta2");
        
        
// uncomment the next line to see debug messages
        // $soapclient->debug_flag = 1;
        
        // set up an array containing input parameters to be
        // passed to the remote procedure
        
$params = array(
                       
'key' => 'Your Google License KEY',          // Google license key
                       
'phrase'   => $_GET['q']                     // search term to check
        
);
        
        
// invoke the method on the server
        
$result $soapclient->call("doSpellingSuggestion"$params"urn:GoogleSearch""urn:GoogleSearch"); 
        
        
// print the results of the search
        
if ($result)
        {
            if (
is_array($result) && $result['faultstring']) {
                echo 
"<h2>Error</h2>\n".$result['faultstring'];    
            } else {
                echo 
"Google suggested <span style=\"font-style:italic;font-weight:bold;\"><a href=\"?q=" urlencode($result) . "\">" $result "</a></span> for the term <strong>" $_GET['q'] . "</strong>";
            }
        }
}

function 
GoogleSearch()
{
        
// create a instance of the SOAP client object
        
$soapclient = new soapclient("http://api.google.com/search/beta2");
        
        
// uncomment the next line to see debug messages
        // $soapclient->debug_flag = 1;
        
        // set up an array containing input parameters to be
        // passed to the remote procedure
        
$params = array(
             
'key' => 'Your Google License KEY',   // Google license key
             
'q'   => $_GET['q'],                          // search term
             
'start' => 0,                                  // start from result n
             
'maxResults' => 10,                            // show a total of n results
             
'filter' => true,                              // remove similar results
             
'restrict' => '',                              // restrict by topic
             
'safeSearch' => true,                          // remove adult links
             
'lr' => '',                                    // restrict by language
             
'ie' => '',                                    // input encoding
             
'oe' => ''                                     // output encoding
        
);
        
        
// invoke the method on the server
        
$result $soapclient->call("doGoogleSearch"$params"urn:GoogleSearch""urn:GoogleSearch"); 
    
        
// print the results of the search
        
if ($result['faultstring']) {
            echo 
"<h2>Error</h2>\n".$result['faultstring'];    
        } else {
            
//print_r($result);
            //echo "<hr />";
            
echo "<h2>Search Results</h2>\n";
            
            if (
is_array($result['directoryCategories'])) { // If directoryCategory exists, write it
                
foreach ($result['directoryCategories'] as $dc) {
                    echo 
"<a href=\"http://directory.google.com/".$dc['fullViewableName']."\" target=\"_blank\">".$dc['fullViewableName']."</a><br />\n";
                }
            }
            
            echo 
"Your search for <em>".$result['searchQuery']."</em> produced ".$result['estimatedTotalResultsCount']." hits.<br />\n";
            
            echo 
"<dl>\n";
            
            if (
is_array($result['resultElements']))
            {
                foreach (
$result['resultElements'] as $r)
                {
                    echo 
"<dt><a href=" $r['URL'] . ">" $r['title'] ."</a></dt>\n";
                    echo 
"<dd>".$r['snippet'] . "(" $r['cachedSize'] . ")\n";
                    echo 
"<br/>\n".$r['URL']."<br/>\n";
                    echo 
"<a href=\"cached/?q=".urlencode($result['searchQuery'])."&amp;u=".$r['URL']."\">Get cached page</a>\n";
                    echo 
"</dd>\n";
                }
            }
            
            echo 
"</dl>";
            
            echo 
"This is result 1-10 of your search for <em>".$result['searchQuery']."</em>. There are ".$result['estimatedTotalResultsCount']." total hits.<br />\n";
            echo 
"<a href=\"http://www.google.com/search?q=".urlencode($result['searchQuery'])."&amp;start=10\" target=\"_blank\">Click here to browse the next 10 results</a><br />\n";
        }
}
?>

        <h2>Google API Demo</h2>

                    <form action="" method="get" style="text-align:right;padding:5px;">
                    Search: <input type="text" name="q" alt="Search Field" value="<?php if (isset( $_GET['q'] )) { echo $_GET['q']; } ?>" />
                    <br />
                    <input type="submit" value="Search Google" style="margin:5px;" />
                    </form>
                      <p style="font-size:.8em;">
                    This is merely a demonstration of the Google API.<br />
                    The API is in Beta so results may not be as accurate as <a href="http://www.google.com">
                    Google.com</a>
<?php
            
if (isset($_GET['q'])) {
                echo 
"<p>";
                
GoogleSpellCheck();
                echo
"</p>";
                
                
GoogleSearch();
            }
?>