Example Application: Multiple Domain Search


One of the limitations of a Google search is that you can only restrict your search to one domain at a time. This isn't usually a problem, but there are several instances where this isn't ideal. For example, you may want to allow users to search both the corporate pages and the community forums at the same time; this could pose a significant problem if the corporate pages were hosted at example.com, while the community forums were held at communityexample.com. The site restriction would not allow you to complete that search. To take another example, say you wanted users to search www.example.com and forums.example.com, but not beta.example.com or people.example.com — again the restrictions of the search do not allow for this functionality.

There are a couple options when dealing with the multiple result sets that will be generated when performing searches like these. Unfortunately, Google does not provide a match percentage with results, so the attractive option of mixing results as indicated by their percentage isn't available. An alternative would be to fold the results together, one from one set, one from the other, and so on. The approach taken for this example, however, is to present the results from the primary domain first, then in a graphically identifiable manner, and the results from the community forums second, so they are available, but distinct.

Overall, this code does not introduce many new features, but it does present a complete, ready-to-use example showing how the Google API can be used. I have removed the templating code, because it is quite site specific.

Features:

  • Errors are reported to an administrator, rather than the end user

  • Queries are cached to save on keyword use

  • Results are fully templated for integration into the overall site

  • Results are sent through a jump page for monitoring

  • Jump page uses md5() to screen IP address

search.php:

 <?php require ("../common_db.php"); $admin = "$adminJoe@example.org"; if (isset($_GET['query'])) {   require('../lib/nusoap.php');   $searchQuery = html_entity_decode($_GET['query']);   $start = $_GET['start'];   //$client = new soapclient("http://api.google.com/search/beta2", false);   $client = new soapclient("http://example.preinheimer.com/google/googleapi/GoogleSearch.wsdl", true);   if ($client->getError())   {     $error = "Error creating client " . $client->getError() . "\n";     echo "An error was encountered while trying to fulfill your request, please try again later.";     mail($admin, "Error creating client object", "$error");     exit;   }   $client->soap_defencoding = 'UTF-8';   $mainResult = getGoogleResults(&$client, "site:www.example.com ". $searchQuery, $start);   $forumResult = getGoogleResults(&$client, "site:forum.example.com ". $searchQuery, $start);   $suggestions = getSuggested($searchQuery);   if ($client->fault)   {     echo "An error was encountered while trying to fulfill your request, please try again later.";     ob_start();     print_r($result);     $error = ob_get_clean();     mail($admin, "Client Fault", "$error");   } else {     if ($client->getError())     {                 echo "An error was encountered while trying to fulfill your request, please try again later.";            mail($admin, "Client Fault", $client->getError());          } else          {                 $searchQuery = htmlentities($searchQuery);          echo "<b>Search Query</b>: <i>" . $searchQuery . "</i><br>";       $x = $mainResult['startIndex'];       $y = $mainResult['endIndex'];       $queryResults = $mainResult['resultElements'];       if (count($queryResults) > 1)       {         foreach($queryResults as $item)         {           echo "<a href=\"{$item['URL']}\">{$item['title']}</a><br>\n";           echo $item['snippet'] . "<br><br>\n";         }       }else       {         echo "No results to display";       }       echo "Search results from our community forum, note that example.com is not       responsible for the content provided in the community forums<br><br>";       $x = $forumResult['startIndex'];       $y = $forumResult['endIndex'];       $queryResults = $forumResult['resultElements'];       if (count($queryResults) > 1)       {         foreach($queryResults as $item)         {           echo "<a href=\"{$item['URL']}\">{$item['title']}</a><br>\n";           echo $item['snippet'] . "<br><br>\n";         }       }else       {         echo "No results to display";       }       $nextStart = $mainResult['endIndex'];       echo "<br><br>";       echo "<a href=\"./nusoap.simple.php?query={$searchQuery}&start=$nextStart\">Next 10 Results</a>";     }   } } function runGoogleSearch($client, $searchQuery, $start) {    $query = array(          'key'=>'u6U/r39QFHK18Qcjz/XdWSbptVaj9k1t',          'q'=> $searchQuery,          'start'=>$start,          'maxResults'=>10,          'filter'=>true,          'restrict'=>'',          'safeSearch'=>true,          'lr'=>'',          'ie'=>'',          'oe'=>''   );   $result = $client->call("doGoogleSearch", $query, "urn:GoogleSearch", "urn:GoogleSearch");   return $result; } function getGoogleResults($client, $searchQuery, $start) {   $key = md5($start . $searchQuery);   // Check for recent items   $query = "SELECT * FROM 06_google_cache_meta WHERE `key` = '$key' AND ((NOW() - `time`) < 84600)";   $results = getAssoc($query);   print_r($results);   if (count($results) > 0)   {     echo "Using Cached Data";     //Cache exists and is recent, Create object to return     $result = array();     $result['estimateIsExact'] = $results['estimateIsExact'];     $result['estimatedTotalResultsCount'] = $results['estimatedTotalResultsCount'];     $result['startIndex'] = $start + 1;     $searchResultQuery = "SELECT * FROM 06_google_cache WHERE `query` = '$searchQuery' AND `start`= '$start'";     $searchResults = getAssoc($searchResultQuery);     $result['endIndex'] = $start + count($searchResults);     $result['resultElements'] = $searchResults;     return $result;   }else   {     //Save results     $result = runGoogleSearch(&$client, $searchQuery, $start);     if ($client->fault)     {          return $result;     } else {          if ($client->getError())         {                return $result;          } else          {            $linkID = db_connect();                 $queryResults = $result['resultElements'];                 $query = mysql_escape_string($searchQuery);                 $index = 0;                 $insertQuery = "REPLACE INTO 06_google_cache_meta                  (`key`, `query`, `start`, `estimateIsExact`, `estimatedTotalResultsCount`, `time`)                 VALUES                  ('$key', '$query', '$start', '{$result['estimateIsExact']}', '{$result['estimatedTotalResultsCount']}', null)";                 insertQuery($insertQuery);                 if (count($queryResults) > 1)                 {           foreach($queryResults as $item)           {             $url = mysql_escape_string($item['URL']);             $snippet = mysql_escape_string($item['snippet']);             $title = mysql_escape_string($item['title']);             $key = md5($start . $index . $query);             $insertQuery = "REPLACE INTO 06_google_cache             (`key`, `index`, `query`, `start`, `snippet`, `title`, `url`)             VALUES             ('$key', '$index', '$query', '$start', '$snippet', '$title', '$url')";             replaceQuery($insertQuery, $linkID);             $index++;           }         }         return $result;       }     }   } } function getSuggested($query) {   $suggestions = array();   $query = explode("", $query);   $linkID = db_connect();   foreach($query AS $word)   {     $word = mysql_real_escape_string($word, $linkID);     $query = "SELECT * FROM 06_google_suggest WHERE `word` = '$word'";     $suggest = getAssoc($query, 2);     if (count($suggest) > 0)     {       foreach ($suggest as $suggestion)       {         $suggestions[] = $suggestion;         echo "Added a suggestion";       }     }   }   return $suggestions; } ?> 

jump.php:

 <?php require("../common_db.php"); $linkID = db_connect(); $DIRTYtarget = $_GET['t']; $query = mysql_real_escape_string($_GET['q'], $linkID); $target = mysql_real_escape_string($DIRTYtarget, $linkID); $ip = md5($_SERVER['REMOTE_ADDR']); $query = "INSERT INTO 06_google_cache_clicked (time, query, url, ip)   VALUES(null, '$query', '$target', '$ip')"; insertQuery($query); header("Location: $DIRTYtarget"); ?> 

The resulting output, matched to your site's overall design, will easily allow you to present consistent results from multiple domains either internal or external to your firm. By combining these results in one location overall, you're presenting a very powerful tool, and thanks to Google's help you're doing it without a lot of code.




Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
Professional Web APIs with PHP. eBay, Google, PayPal, Amazon, FedEx, Plus Web Feeds
ISBN: 764589547
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net