PHP Classes

File: SearchEngine.php

Recommend this page to a friend!
  Classes of Jonathan Schmidt-Dominé   GoogleCrawler   SearchEngine.php   Download  
File: SearchEngine.php
Role: Example script
Content type: text/plain
Description: Example-Search-Engine with thumbshots
Class: GoogleCrawler
Perform searches and retrieve results from Google
Author: By
Last change: Better interface for Google Crawler (additional get-parameters for columns, images and pagination)
Date: 13 years ago
Size: 5,553 bytes
 

Contents

Class file image Download
<?
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * (C) Jonathan Schmidt-Dominé 2008-2011 < devel@the-user.org >
 */
header('content-type:text/html;charset="utf-8"');
echo
'<?xml version="1.0"?>';
?>
<!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" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Q: <?= htmlspecialchars($_GET['searchquery']) ?></title>
</head>
<body>
<form action="">
<div>
<input type="text" name="searchquery" size="25" value="<?=htmlspecialchars($_GET['searchquery'])?>"/>
<input type="hidden" name="withimages" value="<?=htmlspecialchars($_GET['withimages'])?>"/>
<input type="hidden" name="numcols" value="<?=htmlspecialchars($_GET['numcols'])?>"/>
<input type="hidden" name="navinum" value="<?=htmlspecialchars($_GET['navinum'])?>"/>
<input type="hidden" name="resultsperpage" value="<?=htmlspecialchars($_GET['resultsperpage'])?>"/>
<input type="submit" value="Search"/>
</div>
</form>
<?
if(!empty($_GET['searchquery']))
{
    require_once
"GoogleCrawler.class.php";
   
$resultsperpage = !empty($_GET['resultsperpage']) ? max(1,intval($_GET['resultsperpage'])) : 18;
   
$navinum = !empty($_GET['navinum']) ? max(1,intval($_GET['navinum'])) : 15;
   
$withimages = !empty($_GET['withimages']) ? (bool)($_GET['withimages']) : false;
   
$numcols = !empty($_GET['numcols']) ? max(1,intval($_GET['numcols'])) : ($withimages ? 2 : 3);
   
$site = isset($_GET['site']) ? max(0,intval($_GET['site'])) : 0;
   
$gC = new GoogleCrawler($_GET['searchquery'], $site, $resultsperpage, (isset($_GET['lang']) ? $_GET['lang'] : 'en'), 'http://www.google.'.(isset($_GET['domain']) ? $_GET['domain'] : 'com').'/cse');
   
ob_start();
    echo
'<div class="pages">
'
;
   
$numpages = ceil($gC->numresults / $resultsperpage) - $resultsperpage + 1;
    {
       
$pages = array();
       
$pages[1] = '<a href="?searchquery=' . urlencode($_GET['searchquery']) . '&amp;site=1&amp;resultsperpage=' .$resultsperpage . '">1</a>';
       
$pages[2] = '...';
        for(
$i = (($site > $navinum ? $site: $navinum+1) - $navinum); $i < $site; ++$i)
           
$pages[$i] = '<a href="?searchquery=' . urlencode($_GET['searchquery']) . '&amp;site=' . $i . '&amp;resultsperpage=' . $resultsperpage .'">' . $i . '</a> ';
       
$pages[$i] = '<span class="actual">' . $i . '</span>';
        for(++
$i; $i < min($site+$navinum+2, $numpages); ++$i)
           
$pages[$i] = '<a href="?searchquery=' . urlencode($_GET['searchquery']) . '&amp;site=' . $i . '&amp;resultsperpage=' . $resultsperpage .'&amp;numcols=' . $numcols . '&amp;withimages=' . $withimages . '&amp;navinum=' . $navinum . '">' . $i . '</a>';
        echo
implode(",\n", $pages);
    }
    echo
'</div>
'
;
    echo
'<h3>Total: ' . $gC->numresults . ' results.</h3>';
   
$navigation = ob_get_clean();
   
    echo
$navigation;
   
    echo
'<style type="text/css">
.result { width: '
. (100.0/$numcols) . '%; }
'
. ($withimages ? 'a img { border: 0; }
'
: '') . '</style>';
   
    echo
'<table>';
    if(
$withimages)
    {
        foreach(
$gC->results as $i => $result)
        {
            if(
$i % $numcols == 0)
                echo
'<tr>';
            echo
'<td class="result"><table><tr><td colspan="2">
<a href="'
. htmlspecialchars($result['url']) . '"><h3>' . $result['title'] . '</h3></a></td></tr>
<tr><td><a href="'
. htmlspecialchars($result['url']) . '"><img alt="Preview: ' . htmlspecialchars($result['url']) . '" src="http://image.picoshot.com/thumbnail.php?url=' . urlencode($result['url']) . '"/></a></td>
<td>'
. $result['description'] . '
'
;
            if(
$result['cache-url'] !== null)
                echo
'<h5><a href="' . htmlspecialchars($result['cache-url']) . '">Cached version</a></h5>
'
;
            echo
'</td></tr></table></td>
'
;
            if(
$i % $numcols == $numcols - 1)
                echo
'</tr>';
        }
        if(
count($gC->results) % $numcols != 0)
            echo
'</tr>';
    }
    else
    {
        foreach(
$gC->results as $i => $result)
        {
            if(
$i % $numcols == 0)
                echo
'<tr>';
            echo
'<td class="result"><table><tr><td>
<a href="'
. htmlspecialchars($result['url']) . '"><h3>' . $result['title'] . '</h3></a></td></tr>
<tr><td>'
. $result['description'] . '
'
;
            if(
$result['cache-url'] !== null)
                echo
'<h5><a href="' . htmlspecialchars($result['cache-url']) . '">Cached version</a></h5>
'
;
            echo
'</td></tr></table></td>
'
;
            if(
$i % 3 == 2)
                echo
'</tr>';
        }
        if(
count($gC->results) % $numcols == $numcols - 1)
            echo
'</tr>';
    }
    echo
'</table>';
    echo
$navigation;
}
?>
<body>
</html>