PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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