Adding Google Business search to your ecommerce website
July 19, 2007
The internet is abuzz with news of Google's new business custom search engine. Google is allowing business sites to have their own custom search engine for $100 per year (<5,000 pages) or $500 per year (up to 50,000) pages. This includes some cool reporting and an XML API so that you can customize to your heart's content.

After messing around with Google's javascript code I decided to write my own little script to better integrate it into my website. The javascript code was giving me some major problems with the width of my pages, so I canned it and went to their XML version of the search function.
The following is a very simple php & html script that you can use for your own Google business search engine. It was made for a php5 server as it uses the built-in simpleXML class. Save this in a separate file, configure one or two lines based on your Google search account, use php to include it between in the body of your site, and that's it.
It currently uses two tables, but you can easily change this to a division based layout. I used tables only so I didn't have to use external css, or mess with division styling that might not work universally. Also, you can post to this page from a form anywhere on your site as long as the text field is names query and you are using the GET method to submit the form. Lastly, I would set error_reporting to E_ERROR, as E_WARNING may generate some few warnings.
Here we go:
The zip file has a version with correct tabs and the GPL license and is located below this code. Also, I rarely distribute code that I write, so I apologize in advance for any poor commenting.
Save this file as googleSearch.php or something similar. Edit the $accountId variable with your information, and change the form action="" to the page on your site that you will include this file on (IE: The actual search page you want to use on the site: search.php, site-search.php, etc.).
-
<!-- Start Search Box -->
-
<table width="100%" border="0" cellspacing="0" cellpadding="10">
-
<tr>
-
<td align="center" valign="middle">
-
-
<form action="site-search.php">
-
<input name="query" type="text" size="40" value="<?php if(isset($_GET['query'])){echo urldecode(cleanSearch($_GET['query']));} ?>" />
-
<input type="submit" name="sa" value="Search" />
-
<img src="http://google.com/coop/images/google_custom_search_smnar.gif" alt="Google Custom Search" />
-
</form>
-
-
</td>
-
</tr>
-
</table>
-
<!-- End Search Box -->
-
-
<?php
-
-
//constants
-
$accountId = ''; //put google search ID Here - Should be a long string of numbers letters and symbols
-
$collation = 'ISO-8859-1'; //change this if you want something other than ISO-8859-1, IE: ut8-f
-
$numberPerPage = '10'; //this is how many results you want per page
-
$noResults = 'There were no results found for your search. Please try again.'; //Message if no results are found
-
-
//check if the form was submitted
-
-
//clean the input so that it is safe
-
$searchTerm = cleanSearch($_GET['query']);
-
-
//cast $_GET['start'] to an integer just in case
-
$start = (int)$_GET['start'];
-
-
//build the google query here
-
$buildQuery = 'http://www.google.com/search?cx='.$accountId.'&client=google-csbe&start='.$start.'&num='.$numberPerPage.'&output=xml_no_dtd&q='.$searchTerm.'&ie='.$collation.'&oe='.$collation;
-
-
//get the result for the query from google
-
-
//instantiate a simple xml class and process the google result
-
$xml = new SimpleXMLElement($thisSearch);
-
-
//find the number of results for pagination later on
-
-
//Display Nothing found message if there are no results.
-
if($total <1){
-
-
$output .= "<p>$noResults</p>";
-
-
}
-
-
//loop through the results
-
foreach ($xml->RES->R as $key){
-
-
//the replaceChars removes anything you need it to, I found google returning some unusable characters, and you can fix it with this function
-
//Edit the actual find / replace at the bottom
-
-
//format the output for each result
-
$output .= '
-
<p><a href="'.$key->U.'">'.replaceChars($key->T).'</a><br />
-
'.replaceChars($key->S).'<br />
-
<span style="color:#060;">'.$key->U.'</span></p>
-
';
-
-
}
-
-
//simple pagination
-
$output .= '
-
<table width="100%" border="0" cellspacing="0" cellpadding="5">
-
<tr>
-
';
-
-
//make sure we dont display previous page on the first page
-
if ($start> 0){
-
-
$output .= '<td align="left"><a href="?query='.$searchTerm.'&start='.($start - $numberPerPage).'">« Previous 10</a></td>';
-
-
}
-
-
//make sure we dont display next page on the last page
-
if ($total == $numberPerPage){
-
-
$output .= '<td align="right"><a href="?query='.$searchTerm.'&start='.($start + $numberPerPage).'">Next 10 »</a></td>';
-
-
}
-
-
$output .= '
-
</tr>
-
</table>
-
';
-
-
echo $output;
-
-
}
-
-
function cleanSearch($input){
-
//this will clean the query so that it is safe to use for whatever
-
//this is broken into steps, but can be done on a single line if prefered
-
-
//trim whitespace
-
-
//if you would like your users to be able to search for code, you can comment this out
-
//Make sure you know what you're doing before you comment this out!
-
-
//Convert html characters to a safe format, IE: & - & etc.
-
-
//convert to a url safe code since we use the GET function to request from google
-
-
return $input;
-
}
-
-
function replaceChars($input){
-
//using this function to replace some html from the data returned by google
-
//Make sure there is a corresponding value in the find and replace arrays or you will have problems
-
-
//these are what you want to replace
-
'»',
-
'<br>'
-
);
-
-
//these are what you need to replace them with
-
'»',
-
''
-
);
-
-
-
}
-
-
?>
Example of how to implement it:
This is the actual page that you will submit to. Name it something like search.php and upload it with the googleSearch.php file to your server.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
-
<title>My Custom Search</title>
-
</head>
-
-
<?php include "googleSearch.php"; ?>
-
</body>
-
</html>
Download this file along with it's license and correct tab layout. http://www.ecommerce-blog.org/googleSearch.zip
Related information:
Official Google Custom Search Blog
The Benefits of Using Google's Custom Search Engine - seobook.com
Google Custom Search Engine for Businesses - online-tech-tips.com
**If you notice any errors or security issues with any of the code, or bugs in general let me know and I will update this script.
Comments
19 Responses to “Adding Google Business search to your ecommerce website”
Got something to say?





[...] Ecommerce Blog has written a script to help you add the new custom Google business search to your [...]
[...] eCommerce Blog has posted a simple script to help you add a Google business search engine to your own [...]
Updated the script to add support for no results found. Sep 19, 2007
Do you have a example ?
I’m using this script on my main business website here:
http://www.merchantequip.com/
hi,
i found error on my search box :
” site-search.php was not found on this server.”
and what a code site-search.php ? im found file on googleSearch.zip just ‘googlesearch.php’
let me know
On this page, copy the bottom code box, paste it into an html file and name it site-search.php. The download has been updated as well.
Being a complete beginner at PHP, I’ve been hoping to find a script just like yours so that I can add an XML implementation of Google Search to our company’s website. Unfortunately, our web host runs PHP 4 and we don’t have the option of upgrading. In trying to figure out why the script wasn’t working, I determined that SimpleXMLElement doesn’t exist in PHP 4, which I’m hoping is the only part of the script that is incompatible.
Because I can’t find any other scripts that do this, it looks like I’ll have to get a book and learn PHP. I’m curious about one thing though: Is XML parsing in PHP4 similar enough to PHP5 that I’ll just be able to make some modifications to your script or is everything so different that I’ll have to try to rewrite the script from the ground up?
Thanks,
Bryan
You should definitely see if you host plans to upgrade their php, and try to get them to if they aren’t planning it. PHP4 is past its End of Life as php6 is just around the corner. They may also have servers with php5 on them that they can transfer your domain to with little difficulty.
As far as using it with php4, I would look around for a php4 class that mimics the simpleXML class. If I come across one that looks decent, I’ll post it up here.
First of all thanks for the tip. I was able to find a class that mimics PHP5’s SimpleXML here: http://www.criticaldevelopment.net/xml/. There was still some programming required to get your code to work with the class because the class is still not as easy as SimpleXML and “foreach” loops don’t work for objects in PHP 4, either, but I managed to get it up and running.
Also, I wanted to let you know that I might have found a bug in the code. I noticed that searches that are enclosed in quotes, such as "hello there" alway return 0 results. The problem is the “$input = htmlspecialchars($input);” line in the cleanSearch function. It’s screwing up the quotes so that Google doesn’t recognize them. I commented out that line and it fixed the problem. I don’t notice any negative effects of removing that line, but if you’re aware of some then it might be worthwhile to rewrite that portion so that quotation marks are left alone.
Thanks again,
Bryan
Very relevant discussion. We would also like to run your script on our website, but it is, unfortunately, written in PHP4.
So i was thinking - Bryan do you plan on sharing your modified script with the uneducated masses like me?
Your goodwill would be much appreciated.
Thanks
Artas
thanks
Thanks for the script!
hi
can you design i for php 4?
can i use this with free version of custom search?
i want to use it here:
http://www.rapid2share.com
tnx for your good app
Worked perfectly. Thanks!
its very useful. thanks
Hello Friend,
I am Geeting a Warning (Warning: Invalid argument supplied for foreach() in C:\Domain\sufiyacollege.com\wwwroot\google\ on Line 80. Plz Tell me How i Can Fix This Problem. and Also my Search Geeting “There are No Result” Error.
Thank you.
hi i want to tell me about the tax. tax all type of tax sale tax income tax
You should definitely see if you host plans to upgrade their php, and try to get them to if they aren’t planning it. PHP4 is past its End of Life as php6 is just around the corner. They may also have servers with php5 on them that they can transfer your domain to with little difficulty.
As far as using it with php4, I would look around for a php4 class that mimics the simpleXML class. If I come across one that looks decent, I’ll post it up here.