Archive for October, 2005

Search Google using PHP 5

October 3rd, 2005

There are quite a few examples of using the SOAP package within PEAR to search Google. While the first example I saw worked fine, upgrades to the SOAP package have added some problems searching Google.

SOAP-0.8.1 works as directed, but upgrading to SOAP-0.9.1 breaks searching Google through SOAP.

I have answered about 3 emails on the PEAR mailing list related to the SOAP-0.9.1 problems searching Google, and I decided to look into solutions a little more.

If you have PHP 5 with the SOAP extensions installed you can remove the PEAR SOAP package out of the picture entirely.

Here’s a starting point:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Google Search Example</title>
<style type="text/css">
.ret_head {height:1.5em;width:100%;border-bottom:1px solid #6C6C66;}
.ret_head .sch_title {font-size:1.5em;float:left;}
.ret_head .ret_count {float:right;}
.err_head {margin-top:20px;margin-left:125px;}
.g {margin-bottom:1em;}
.g .ret_snip {}
.g .ret_url {color:#9E605D;}
</style>
</head>

<body>
<?php
// Google API Key
$key    = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Google Search Query
$query    = 'soap';

$client = new SoapClient('http://api.google.com/GoogleSearch.wsdl');
$ret = $client->doGoogleSearch(
                    
$key,    // the Developer's key
                    
$query,    // the search query
                    
0,    // the point in the search results should Google start
                    
10,        // the number of search results (max 10)
                    
false,    // should the results be filtered?
                    
'',
                    
false,
                    
'',
                    
'',
                    
''
        
);
if (
$ret->estimatedTotalResultsCount>0) {
    echo
"<div class='ret_head'><div class='sch_title'>Google Search</div><div class='ret_count'>Results <strong>{$ret->startIndex}</strong> through <strong>{$ret->endIndex}</strong> of about <strong>{$ret->estimatedTotalResultsCount}</strong> for <strong>".htmlentities($query)."</strong>.</div></div>";
    foreach (
$ret->resultElements as $result) {
        echo
"<div class='g'>\n";
        echo
"<a href='{$result->URL}'>";
            if (
$result->title != "") {
                echo
$result->title;
            } else {
                echo
str_replace("http://","",$result->URL);
            }
        echo
"</a>\n";
        if (
$result->snippet != "") echo "<div class='ret_snip'>{$result->snippet}</div>\n";
        if (
$result->title != "") echo "<div class='ret_url'>".str_replace("http://","",$result->URL)." - </div>\n";
        echo
"</div>";
    }
} else {
    print(
"<div class='err_head'>Your search - <strong>".htmlentities($query)."</strong> - did not match any documents.<br />
            Suggestions:<br />
            <ul>
                <li>Make sure all words are spelled correctly.</li>
                <li>Try different keywords.</li>
                <li>Try more general keywords.</li>
                <li>Try fewer keywords.</li>
            </ul>
            </div>"
);
}

highlight_file($_SERVER['SCRIPT_FILENAME']);

?>

</body>
</html>

Listed on BlogShares