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
<?php
/* ****************************************************
ClASS FOR GENERATING GOOGLE SEARCH RESULTS
Functions for google result generation
**************************************************** */
?>
<?php
// 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
//FUNCTION #1
//constructor for google search class
function googlesearch( $search_Domain="", $google_SearchTerm=" ", $search_ThisDomain=1, $search_EngineURL="http://www.google.com/search?h=en&q=",$fail_URL="http://www.google.com" ){
// 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' " ;
$this->currentPagingParam=0;
// if searchDomain is not given then set it with current working site domain
if( $this->searchDomain == "" )
$this->searchDomain=$_SERVER['HTTP_HOST'];
}
//FUNCTION #2
//Function to generate a google search link
function generateSearchQuery($keyword){
$paging="";
if( isset( $_GET['start'] ) ){
$paging=intval($_GET['start']);
$this->currentPagingParam=$paging;
}
if($keyword=="")
$newKey=" ";
else{
//replace unwanted spaces with '+'
$keyword=preg_replace("/\s{1,}/","+",$keyword);
$newKey="+";
}
$pagingParam="";
if($paging!="")
$pagingParam="&start=".$paging;
//http://www.google.com/search?q=site%3Awww.sanlokpublications.com+test&btnG=Search&h=en&hl=en&sa=2
if($this->searchThisDomain){
//if user wants to search a particulaar domain then add google site search attributes to link
$currentDomain=$this->searchDomain;
$newKey="site:{$currentDomain}{$newKey}".$keyword."&btnG=Search".$pagingParam;
}else
$newKey=$keyword;
//return generated link
return $this->searchEngineURL.$newKey;
}
//FUNCTION #3
// Function to get contents from the google search page
function fetchContent($query_search_URL){
if($query_search_URL!=""){
//getting contents from Google URL by curl
$content=$this->fetchContentFromURLbyCURL($query_search_URL);
// if no content with curl request then try normal url request
if( trim($content)=="" )
$content=$this->fetchContentFromURL($query_search_URL);
if( $content!="" ){
return $content;
}else{ //if search failed redirect to failURL
$this->failedSearch(2,2);
}
}else{ //if search link is invalid ,search failed redirect to failURL
$this->failedSearch(1,2);
}
}
/* OLD FETCH CONTENT FUNCTION
// Function to get contents from the google search page
function fetchContent($query_search_URL){
if($query_search_URL!=""){
//getting contents
$content=@file_get_contents($query_search_URL);
if( $content!="" ){
return $content;
}else{ //if search failed redirect to failURL
$this->failedSearch(2,2);
}
}else{ //if search link is invalid ,search failed redirect to failURL
$this->failedSearch(1,2);
}
}
*/
//FUNCTION #4
//Function getting contents from Google URL if PHP server allows to access contents froma remote URL
function fetchContentFromURL($qryURL){
$PageContent="";
$PageContent=@file_get_contents($qryURL);
return $PageContent;
}
//FUNCTION #5
//Function getting contents from Google URL using CURL functions
function fetchContentFromURLbyCURL($qryURL){
$PageContent="";
//Initialize the cURL session
$ch = curl_init();
if($ch){
//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 #6
//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;
$resultArray=array();
if( $rawContent != "" ){
$rawContent=preg_replace("/class=[\'\"]?(.?)[\'\"]?/","",$rawContent);
$arrheading=array();
if( preg_match_all("#<a\s{1,}href=[\'\"]?".$this->addSlashesForRegEx($this->searchDomain)."(.+)[\'\"]?(.+)>(.+)<\/a>#U",$rawContent,$matches) ){
$arrheading=$matches[0];
}
if( count($arrheading) > 0 ){
$styledheading=array();
$i=0;
foreach( $arrheading as $linkVal){
$styledURL=preg_replace("#(<a\s{1,}href=[\'\"].+[\'\"]\s?)(.)*(\s+>)#U","$1 {$head_style} $3",$linkVal);
$styledheading[$i]=$styledURL;
$i++;
}
$arrheading=NULL;
$arrheading=$styledheading;
$arrDesc=array();
if( preg_match_all("#<a\s{1,}href=[\'\"]?".$this->addSlashesForRegEx($this->searchDomain)."(.+)[\'\"]?(.+)>(.+)<\/a>(.+){$this->searchDomain}#U",$rawContent,$matches1) ){
$arrDesc=$matches1[0];
}
if( count($arrDesc) > 0 ){
$arrURLs=array();
for($i=0;$i<count($arrheading);$i++){
if( preg_match("#href=[\'\"]?(.+)[\'\"]?\s{1,}#U",$arrheading[$i],$arrhead) )
$linkURL=str_replace("\"","",$arrhead[1]);
$arrURLs[$i]=$linkURL;
}
for($i=0;$i<count($arrheading);$i++){
//yellow marking the search term if needed
if($markSearchTerm){
$arrFind=array("<em>","</em>");
$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>");
}else{
$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;
}else{
return $resultArray;
}
}else{
return $resultArray;
}
}else
return $resultArray;
}
//FUNCTION #7
// Function to get the paging links
function getPaging($strContent,$linkStyle){
$pagelink="";
if( preg_match_all("/start=(\d{1,})/",$strContent,$mtch) ){
$arrMatch=$mtch[1];
if( !@in_array(0,$arrMatch) ) $arrMatch[count($arrMatch)]=0;
sort($arrMatch);
$arrunique=array_unique($arrMatch);
$i=1;
//get currentpage
$currentPage=$_SERVER['PHP_SELF'];
foreach( $arrunique as $val ){
if( $val==$this->currentPagingParam )
$pagelink.="<a href='{$currentPage}?ser={$this->googleSearchTerm}&start={$val}' {$linkStyle}><font color=orange>{$i}</font></a> ";
else
$pagelink.="<a href='{$currentPage}?ser={$this->googleSearchTerm}&start={$val}' {$linkStyle}>{$i}</a> ";
$i++;
}
}
return "<div style='float:right;horizontal-align:right;'>".$pagelink."</div>";
}
//FUNCTION #8
//Function to start search
function startSearch($key,$searchCurrentDomain=1,$head_style="",$desc_style="",$cite_style="",$paging_style="",$markSearchTerm=1){
$paginglinks="";
$this->googleSearchTerm=$key;
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);
//echo($searchQry);
//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>");
echo($paginglinks."<br><br>\n\n");
for($i=0;$i<count($searchContent) ;$i++ )
echo("<div>\n".$searchContent[$i]."\n</div>\n\n");
echo($paginglinks);
}else
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 #9
// 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 )
$searchDomain="http://".$searchDomain;
//escape the RegEx quantifiers in string
$arrfind=array('/',".","-");
$arrReplace=array("\\/","\\.","\\-");
return( str_replace($arrfind,$arrReplace,$searchDomain) );
}
//FUNCTION #10
//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
}
?>
HOW TO ACCESS THIS CLASS?
<?php
//SAMPLE USAGE OF THIS CLASS
//startSearch( keyword,searchOnlyCurrentDomain,headStyle,descriptionStyle,siteURLStyle,pagingLink,NeedtoHiglightSearchTerm)
//sample styles
echo("<style>
.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}
</style>");
//uncomment below include stmt when calling the class from other page
//include_once("class.googlesearch.php");
//set domain for searching [option]
//if search domain is not given, the application take the running server address "www.yourserver.com"
$searchDomain="www.yoursitesdomain.com";
//create object for the search class
$obj= new googlesearch($searchDomain);
$searchTerm="default search term"; //default keyword optional
if( isset($_GET['ser']) ){
if( $_GET['ser'] != "" )
$searchTerm=$_GET['ser'];
}
//interface
echo("<table><tr><td>Search Google</td></tr><tr><td><form method=\"get\" action=\"\"><input type=\"text\" name=ser value=\"");
echo((isset($searchTerm))?$searchTerm:"");
echo("\" /> <input type=\"submit\" name=btn value=\"Search\"/></form></td></tr><tr><td>");
$obj->startSearch($searchTerm,1,"linkgoogle","txt","cite","pagelink",1);
echo("</td></tr></table>");
?>
TO DOWNLOAD THE SCRIPT VISIT PHPCLASSES.ORG
http://www.phpclasses.org/package/5996-PHP-Search-a-site-and-retrieve-results-from-Google.html
Comments
Post a Comment