Skip to main content

Google search integration class - php class

This class can be used to search a site and retrieve results from Google.

It can send HTTP requests to Google search site to perform searches for given keywords restricted to a given site domain.

The class retrieves the result pages, extract the results and display them with configurable presentation styles.

CLASS FILE SCRIPT : save it as class.googlesearch.php

/* ****************************************************

Functions for google result generation
**************************************************** */

// Class for showing google search result on a webpage by taking the
// content form google search page and processing it for other websites

Class googlesearch {
//Start Class

//class variabes
var $googleSearchTerm; // User Typed Search terms
var $searchThisDomain; // Need a Global or local search
var $searchEngineURL; // URL of search Engine you need
var $searchDomain; // Which domain You need to search
var $failURL; // If failed to fetch contents for search page then redirect to this URL
//styling variables
var $headerStyle; // Style for header in result display
var $descriptionStyle; // Style for Description in result display
var $citeUrlStyle; // Style for Cite URL in result display
var $pagingStyle; // Style for Paging links in result display
var $currentPagingParam ;// current paging value


//constructor for google search class
function googlesearch( $search_Domain="", $google_SearchTerm=" ", $search_ThisDomain=1, $search_EngineURL="",$fail_URL="" ){
// all parameters are optional
$this->googleSearchTerm = $google_SearchTerm ;
$this->searchDomain = $search_Domain ;
$this->searchThisDomain = $search_ThisDomain ;
$this->searchEngineURL = $search_EngineURL;
$this->failURL = $fail_URL;

$this->headerStyle = " style='color:#669933;font-family:Arial, Helvetica, sans-serif;font-size:15px' " ;
$this->descriptionStyle = " style='color:#CC3333;font-family:Arial, Helvetica, sans-serif;font-size:13px' " ;
$this->citeUrlStyle = " style='color:#009900;font-family:Arial, Helvetica, sans-serif;font-size:13px;font-style:italic' " ;
$this->pagingStyle = " style='color:red;text-decoration: none;font-family:arial;font-size:12px' " ;

// if searchDomain is not given then set it with current working site domain
if( $this->searchDomain == "" )



//Function to generate a google search link
function generateSearchQuery($keyword){
if( isset( $_GET['start'] ) ){

$newKey=" ";
//replace unwanted spaces with '+'


//if user wants to search a particulaar domain then add google site search attributes to link
//return generated link
return $this->searchEngineURL.$newKey;


// Function to get contents from the google search page
function fetchContent($query_search_URL){

//getting contents from Google URL by curl

// if no content with curl request then try normal url request
if( trim($content)=="" )

if( $content!="" ){
return $content;
}else{ //if search failed redirect to failURL
}else{ //if search link is invalid ,search failed redirect to failURL

// Function to get contents from the google search page
function fetchContent($query_search_URL){
//getting contents
if( $content!="" ){
return $content;
}else{ //if search failed redirect to failURL
}else{ //if search link is invalid ,search failed redirect to failURL


//Function getting contents from Google URL if PHP server allows to access contents froma remote URL
function fetchContentFromURL($qryURL){
return $PageContent;


//Function getting contents from Google URL using CURL functions
function fetchContentFromURLbyCURL($qryURL){
//Initialize the cURL session
$ch = curl_init();
//Set the URL of the page or file to download.
curl_setopt($ch, CURLOPT_URL, $qryURL);
// Ask cURL to return the contents in a variable instead of simply echoing them to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the cURL session
$PageContent = curl_exec ($ch);
// Close cURL session
curl_close ($ch);

return $PageContent;


//Function to format the search result
function formatResult($rawContent,$head_style,$desc_style,$cite_style,$markSearchTerm){

//user defined style class or default style
if( $head_style !="" ) $head_style=" class=\"{$head_style}\" "; else $head_style=$this->headerStyle;
if( $desc_style !="" ) $desc_style=" class=\"{$desc_style}\" "; else $desc_style=$this->descriptionStyle;
if( $cite_style !="" ) $cite_style=" class=\"{$cite_style}\" "; else $cite_style=$this->citeUrlStyle;

if( $rawContent != "" ){


if( preg_match_all("#<a\s{1,}href=[\'\"]?".$this->addSlashesForRegEx($this->searchDomain)."(.+)[\'\"]?(.+)>(.+)<\/a>#U",$rawContent,$matches) ){


if( count($arrheading) > 0 ){
foreach( $arrheading as $linkVal){
$styledURL=preg_replace("#(<a\s{1,}href=[\'\"].+[\'\"]\s?)(.)*(\s+>)#U","$1 {$head_style} $3",$linkVal);

if( preg_match_all("#<a\s{1,}href=[\'\"]?".$this->addSlashesForRegEx($this->searchDomain)."(.+)[\'\"]?(.+)>(.+)<\/a>(.+){$this->searchDomain}#U",$rawContent,$matches1) ){

if( count($arrDesc) > 0 ){

if( preg_match("#href=[\'\"]?(.+)[\'\"]?\s{1,}#U",$arrheading[$i],$arrhead) )


//yellow marking the search term if needed

$arrRep=array("<b style='background-color:yellow;'>","</b>");

$resultArray[$i] = str_replace($arrFind,$arrRep,$arrheading[$i]."\n<br>\n<font {$desc_style} >".strip_tags($arrDesc[$i])."</font>\n<br>\n<a href='{$arrURLs[$i]}' target='_blank' {$cite_style} >URL : ".$arrURLs[$i]."</a>\n<br><br>");
$resultArray[$i] = $arrheading[$i]."\n<br>\n<font {$desc_style} >".strip_tags($arrDesc[$i])."</font>\n<br>\n<a href='{$arrURLs[$i]}' target='_blank' {$cite_style} >URL : ".$arrURLs[$i]."</a>\n<br><br>";

return $resultArray;

return $resultArray;

return $resultArray;

return $resultArray;



// Function to get the paging links
function getPaging($strContent,$linkStyle){
if( preg_match_all("/start=(\d{1,})/",$strContent,$mtch) ){
if( !@in_array(0,$arrMatch) ) $arrMatch[count($arrMatch)]=0;

//get currentpage
foreach( $arrunique as $val ){
if( $val==$this->currentPagingParam )
$pagelink.="<a href='{$currentPage}?ser={$this->googleSearchTerm}&start={$val}' {$linkStyle}><font color=orange>{$i}</font></a> ";
$pagelink.="<a href='{$currentPage}?ser={$this->googleSearchTerm}&start={$val}' {$linkStyle}>{$i}</a> ";



return "<div style='float:right;horizontal-align:right;'>".$pagelink."</div>";


//Function to start search
function startSearch($key,$searchCurrentDomain=1,$head_style="",$desc_style="",$cite_style="",$paging_style="",$markSearchTerm=1){
if( $paging_style !="" ) $paging_style=" class=\"{$paging_style}\" "; else $paging_style=$this->pagingStyle;

// considering whether user needs a global or local search
$this->searchThisDomain = $searchCurrentDomain ;
//generate google Search URL
$searchQry = $this->generateSearchQuery($key);
//generate google result
$strContent = $this->fetchContent($searchQry);
$paginglinks=$this->getPaging( $strContent,$paging_style);

$searchContent =$this->formatResult($strContent,$head_style,$desc_style,$cite_style,$markSearchTerm);
//return search results
if( count($searchContent) > 0 ){
echo("<br><b><font color=red size=3 face=arial>Search results : Matching Records for Search term \"".$key."\".</font></b>\n\n<br>");
for($i=0;$i<count($searchContent) ;$i++ )
echo("\n<b><font color=red face=arial size=3 >Matching Records for \"".$key."\" could not be found, search using other keywords.</font></b>");


// Function to add slashes in Search Domain form making RegEx friendly
function addSlashesForRegEx($searchDomain){
//add the protocol to search domain if needed
if( strpos($searchDomain,"http") === false )
//escape the RegEx quantifiers in string
return( str_replace($arrfind,$arrReplace,$searchDomain) );


//Function to handle failed search
function failedSearch($errcode,$redirectInSeconds=0){
if($errcode==1) $errMsg="Sorry, Invalid Search Query.";
elseif($errcode==2) $errMsg="Sorry, Search Result cannot be retrived.";
else $errMsg="";
echo("<br><br><br><br><br><br><b><font color=red face=arial size=2>{$errMsg}</font></b><br><br><br><br><br><br>\n<meta http-equiv=\"refresh\" content=\"{$redirectInSeconds};URL={$this->failURL}\" />");

//End of class




//startSearch( keyword,searchOnlyCurrentDomain,headStyle,descriptionStyle,siteURLStyle,pagingLink,NeedtoHiglightSearchTerm)

//sample styles
.linkgoogle:hover {color:blue;font-weight:bold;text-decoration: none;}
.linkgoogle {color:red;font-weight:bold;text-decoration: none;}
.txt{color:#000000;font-family:Arial, Helvetica, sans-serif;font-size:13px}
.cite{color:#009900;font-family:Arial, Helvetica, sans-serif;font-size:13px;font-style:italic}
.cite:hover{color:red;font-family:Arial, Helvetica, sans-serif;font-size:13px;font-style:bold}
.head{color:#669933;font-family:Arial, Helvetica, sans-serif;font-size:16px}
.pagelink:hover {color:red;text-decoration: none;font-size:11px;font-family:arial;font-weight:bold}
.pagelink {color:black;font-size:11px;font-family:arial;text-decoration: none;font-weight:bold}

//uncomment below include stmt when calling the class from other page

//set domain for searching [option]
//if search domain is not given, the application take the running server address ""

//create object for the search class
$obj= new googlesearch($searchDomain);

$searchTerm="default search term"; //default keyword optional

if( isset($_GET['ser']) ){
if( $_GET['ser'] != "" )

echo("<table><tr><td>Search Google</td></tr><tr><td><form method=\"get\" action=\"\"><input type=\"text\" name=ser value=\"");
echo("\" /> <input type=\"submit\" name=btn value=\"Search\"/></form></td></tr><tr><td>");




Popular posts from this blog

Payback Points - How to redeem - How to merge multiple payback accounts - Block Payback card - Payback customer care

Your SBI Debit card ending with XX0000 is deactivated only for Internet txn.

SBI account holders may have received an SMS with following message, supposed to be from State Bank of India (SBI).

Your SBI Debit card ending with XX0000  is deactivated only for Internet txn. To activate send SMS "SWON ECOM 0000" to 09223966666. No change for ATM/POS usage
** Replace the four Zeros with last 4 digits of your debit card number

Recently many of the SBI account holder has losed their money due to a hi-tech ATM robbery which happened in Thiruvananthapuram, capital city of Kerala.

Joomla and Forum Integration - Integrating Forums to Joomla

Joomla is one of the most popular CMS opensource packages. It is very easier to develop website's using Joomla. You just need to download Joomla package from Joomla's Official website  and install it on your domain and later adding customizations to templates and feature and Your website is ready :). Now a days most websites provides a forum section for it users for discussing various article topics, gathering opinions etc.

Following are some best know forum opensource packages which can be integrated with Joomla and create a new forum experience for users

Urgent Openings for PHP trainees, Andriod / IOS developers and PHP developers in Kochi Trivandrum Calicut and Bangalore. Please Send Your updated resumes to   Read more »
Search This Blog