Quick and Easy Google Site Search - By PHP Expert


The Google API - http://www.google.com/apis/

Advanced Google searches - http://www.google.com/help/refinesearch.html

PHP script "nusoap"  - http://sourceforge.net/projects/nusoap/

There are three steps to creating this site-specific search engine. 1) Get a license key from Google, 2) download the PHP script, "nusoap" and finally, 3) install a short script to initiate the search and format the HTML output.


nusoap is a PHP script that will facilitate use of the Google API. It is available from the URL,  http://sourceforge.net/projects/nusoap/. Go to this site and find the download link about halfway down the page. At the time of writing the latest version was 0.6.7. The file is zipped but don't let that discourage you from using it on a non-Windows server. It works just as well using Apache and Linux as it does using Internet Information Server and Windows. You will, of course have to have PHP installed on your server. If it is not already installed then it is probably time to find a new web host.

There are a number of files compressed into the zipped file but "nusoap.php" is the only one we need to be concerned about. If you are familiar with PHP you may want to have a look at the other files. I will just mention though that the script we develop is a modification of the "client3.php" file.

Google API

API stands for "Application Programming Interface" ? it is simply a means to tap into the Google search engine without having to actually point your browser at their site. It allows you to perform Google searches programmatically.

You may find out about the Google API at http://www.google.com/apis/.  You need not download the example code but you will need to create a Google account and get a license key. This license key will allow you to initiate 1000 searches per day using the Google API. Check your website statistics and if you are getting fewer than 500 visits per day then this number of searches will most likely be more than adequate.

The Code

Let's first create the form that will invoke the search page:

Search this site: <br />
<form method="get" name="search" 
<input type="text" name="criterion" style="width:100px" /><br />
<input style="margin-top:5px" type="submit" value="Submit" />

Insert this code into the page that you intend to search from ? for the sake of easy reference let's call that page "searchfrom.html".

The code that actually performs the search is below (based on the "client3.php" file):

<title>Site Search Page</title>
if(strpos($criterion, """)){
      $criterion = stripslashes($criterion);
      echo "<b>$criterion</b>";
      echo ""<b>$criterion</b>".</p>";
//your site here
$query .= " site:www.yoursite.com";
//your Google key goes here
$key = "yourgooglekey";
//change the value below if you like
$maxresults = 10;
$start = 0;
$parameters = array(
$client = new
$result = $client->call("doGoogleSearch", $parameters,
$searchtime = $result["searchTime"];
$total = $result["estimatedTotalResultsCount"];
if($total > 0){
  $rs = $result["resultElements"];
  for ($i = 0; $i < $total; $i++){
    if (!isset($rs[$i])) break;
    $element = $rs[$i];
    $url = $element["URL"];
    $snippet = $element["snippet"];
    $output.= "<p><a href="$url">".basename($url)."</a> $snippet</p>n";
  echo $output;
  echo "<br /><br />Search time: $searchtime seconds.";
  echo "<br /><br />Nothing found.";

Save this code as "search.php" making sure that you use the file extension "php".

The changes you need to make to this script are shown in bold. Substitute your domain name for "www.yoursite.com" and your Google key for "yourgooglekey" making sure to enclose both items in quotation marks. Change the value of "$maxresults" if you wish to have fewer or more results.

To sum up, we now have three files that need to be located in the same directory, "searchfrom.html", "nusoap.php" and "search.php". Entering a criterion into the textbox on the "searchfrom.html" page will open the "search.php" page and display the results of your search - limited to the specified site. These results will show a snippet of text and a hyperlink to the page on which your criterion appears. Visitors to your site can now easily find items of interest and quickly navigate to them.

Changes & Improvements

Depending upon your knowledge of PHP and HTML this code can be customised and changed in a number of ways. We have already shown how the number of results returned can be adjusted but you might also want to add a pop-up window of search hints for your site visitors. Let them know that they can search for a single word, or a group of words, or a specific phrase if they enclose the expression in quotation marks.

Another useful element returned from Google is the page title. I've put it into the "search.php" page to show how to access it but have commented it out. One possible use for this piece of information would be to limit search results to a specifically named page. I'll let you determine how this might be done.

As you can see our script uses one of the Google advanced search techniques (see http://www.google.com/help/refinesearch.html ), a criterion, the word "site" followed by a semicolon and then a domain name. In the same way as we have done here, you could easily change this code to search instead for specific file types by using something like " filetype:pdf" instead of " site:www.yoursite.com".

You may also want to indicate to visitors that your site search is Google-based. You may certainly do this but check out the terms and conditions of use at http://www.google.com/apis/api_terms.html.

Some Limitations

It might be said that any web surfer could do exactly what is described here by opening a separate instance of their browser and doing a site-specific search themselves. Quite true, but how many people would know how to do this and even if they knew, how many would actually do it? Most web surfers will appreciate the convenience of being provided with a site-specific search capabilities embedded right into your site.

As already mentioned, this is not the solution for a high-traffic site where many searches will be initiated. Nor is it a solution for a newly posted site. Until a site is indexed by Google no search results will be returned. Likewise, recent changes to a site will not be found until the Googlebot visits and registers them.

Additionally, some of the less common character entities are not rendered correctly. I noticed no problem with common entities such as "&lt;" and "&amp;"  but less common ones such as "&hellip;" were simply rendered as question marks.

That said though, using the Google API in conjunction with the "nusoap" script is a quick and easy way to implement a site-specific search. This is particularly useful for sites that are content-rich and it is an excellent additional service to propose to your clients once their newly created site shows up on Google.

No comments

Enter your email address:

Delivered by FeedBurner


 Subscribe in a reader

Tips for optimizing php code by PHP Expert Important SERVER Variables in PHP - By PHP Expert Improved Error Messages in PHP 5 - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert New Object Oriented Features - By PHP Expert Object Overloading in PHP 5 Persistable Classes - By PHP Expert Dynamic Getter/Setter Methods - By PHP Expert New Functions in PHP 5 New Directives - By PHP Expert Exception Handling - By PHP Expert Password Encryption in PHP - By PHP Expert Output Buffering in PHP - By PHP Expert Page Excerpts Using CURL - By PHP Expert Quick and Easy Google Site Search - By PHP Expert Always Be Notified When Google Crawls Your Site - By PHP Expert How to POST Form Data using CURL - By PHP Expert Cryptography for web developers - By PHP Expert Dynamically Loading JavaScript Files - by PHP Expert What is Web 2.0 - By PHP Expert