PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.10 USD)

More information
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (12), JSON (2), MVC (1), MySQL (7), onbeforeunload (1), OOP (1), PHP (28), PhpDoc (1), PostgreSQL (6), Prototype (11), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Searching Google With The Google API

Creating A New Google Frontend

We’re going to combine and adapt the code created in previous steps to build our own Google front end. The main changes we will make to our code here involve retrieving the search term from the form, as well as providing the option for returning pages other than just the first.

Additionally, we’re going to implement the “I’m Feeling Lucky” feature, which Google provides from their main search page. What this does, is automatically visit the first web site found, rather than showing you the search results.

You can put all this code in a file google.php.

Remember that this is very basic and there is a lot of further customization you can do.

Listing 5 google.php
<?php
    $key = 'your google license key';
    $wsdl = 'http://api.google.com/GoogleSearch.wsdl';
    $limit = 10;
    $maxResults = 1000;
 
    $q = trim(isset($_GET['q']) ? $_GET['q'] : '');
 
    if (strlen($q) > 0) {
        require_once('nusoap.php');
 
        $page = isset($_GET['p']) ? $_GET['p'] : 1;
        $page = max(0, $page);
        $start = ($page - 1) * $limit;
 
        $parameters = array('key'        => $key,
                            'q'          => $q,
                            'start'      => $start,
                            'maxResults' => $limit);
 
        $soap = new SoapClient($wsdl, 'wsdl');
        $result = $soap->call('doGoogleSearch', $parameters);
 
        // if the user clicked the "i'm feeling lucky" button
        if (isset($_GET['lucky']) && count($result['resultElements']) > 0) {
            header('Location: ' . $result['resultElements'][0]['URL']);
            exit;
        }
 
        $numResults = $result['estimatedTotalResultsCount'];
        $numPages = min($maxResults / $limit, ceil($numResults / $limit));
        $page = ($result['startIndex'] - 1) / $limit + 1;
 
        // repopulate the $q variable to put back into the form
        $q = $result['searchQuery'];
    }
 
?>
 
<form method="get">
    <input type="text" name="q" value="<?= htmlSpecialChars($q) ?>" />
    <input type="submit" value="Search" />
    <input type="submit" name="lucky" value="I'm Feeling Lucky" />
</form>
 
<?php if (strlen($q) > 0) { ?>
    <?php if ($numResults == 0) { ?>
        <p>
            Your search yielded no results.
        </p>
    <?php } else { ?>
        <p>
            Results <?= $result['startIndex'] ?> to <?= $result['endIndex'] ?>
            of <?= $result['estimatedTotalResultsCount'] ?>
        </p>
 
        <?php foreach ($result['resultElements'] as $row) { ?>
            <h3><a href="<?= $row['URL'] ?>"><?= $row['title'] ?></a></h3>
            <p>
                <?= $row['snippet'] ?>
            </p>
            <p>
                <?= $row['URL'] ?>
            </p>
        <?php } ?>
 
        <hr />
 
        Page:
        <?php for ($i = 1; $i <= $numPages; $i++) { ?>
            <?php if ($i == $page) { ?>
                <strong><?= $i ?></strong>
            <?php } else { ?>
                <a href="google.php?q=<?= urlencode($q) ?>&p=<?= $i ?>"><?= $i ?></a>
            <?php } ?>
        <?php } ?>
    <?php } ?>
<?php } ?>

In This Article


Tagged in , ,