<?php
// ------------------------------------------------------------------------- //
// Interroger les services Web de Google avec SOAP //
// ------------------------------------------------------------------------- //
// Auteur: J-Pierre Dézélus //
// Web: http://www.phpinfo.net/ //
// ------------------------------------------------------------------------- //
?>
<html>
<head>
<meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
<title>Recherche Google avec PHP/SOAP</title>
<style>
body, td, a {font-family: arial,sans-serif; font-size: 13px; }
td { color: #000000; }
a:link, { color: #00000C; }
a:visited { color: #551A8B; }
a:active { color: #000F00; }
.fl:link { color: #6F6F6F; }
.fl:visited { color: #551A8B; }
.fl:active { color: #000F00; }
</style>
</head>
<body>
<?php
// ----------------------------------------------------------------------------
// paramètres de recherche
// ----------------------------------------------------------------------------
$key = 'votre clé ici';
$q = ($REQUEST_METHOD == 'GET')
? $HTTP_GET_VARS['q']
: $HTTP_POST_VARS['q'];
$q = stripslashes(trim($q)); // query terms
$start = (int)$HTTP_GET_VARS['debut']; // zero-based index of the fisrt result
$maxResults = 10; // number of results per page
$filter = false; // automatic filtering
$restrict = 'countryFR'; // country restrict
$safeResearch = true; // no adults site
$lr = 'lang_fr|lang_en'; // language restrict
$ie = 'utf8'; // input encoding
$oe = 'utf8'; // output encoding
// ----------------------------------------------------------------------------
// formulaire de recherche
// ----------------------------------------------------------------------------
?>
<form action="" method="GET">
<input type="text" name="q" value="<?php echo htmlspecialchars($q); ?>" />
<input type="submit" value="OK" />
</form>
<?php
// ----------------------------------------------------------------------------
// exécution de la recherche et exploitation des résultats
// ----------------------------------------------------------------------------
if (strlen($q) > 0)
{
require_once('SOAP_Google.php');
$parametres = Array
(
'query' => $q,
'filter' => $filter,
'restrict' => $restrict,
'safeResearch' => $safeResearch,
'start' => $start,
'maxResults' => $maxResults,
'lr' => $lr,
'ie' => $ie,
'oe' => $oe
);
$google = new SOAP_Google($key);
if ($resultats = $google->search($parametres))
{
echo '<table width="100%" border="0" cellpadding="2" cellspacing="0">';
echo '<tr><td bgcolor="#5A6BA5" nowrap><font size="-1" color="#ffffff">Google a recherché <b>'.$resultats['searchQuery'].'</b> </font></td>';
echo '<td bgcolor="#5A6BA5" align="right" nowrap><font size="-1" color="#ffffff"><b>'.$resultats['startIndex'].'</b> - <b>'.$resultats['endIndex'].'</b> résultats, sur un total d\'environ <b>'.$resultats['estimatedTotalResultsCount'].'</b>. Recherche effectuée en <b>'.round($resultats['searchTime'], 2).'</b> secondes.</font></td></tr>';
echo '</table><br />';
if (sizeof($resultats['resultElements']) > 0)
{
$numero = $resultats['startIndex'];
// réponses
foreach ($resultats['resultElements'] as $reponse)
{
// URL + titre
echo ($numero++).'. <a href="'.$reponse['URL'].'">'.$reponse['title'].'</a><br />';
// extrait
echo $reponse['snippet'].'<br />';
// description ODP
if (($description = $reponse['directoryTitle']) != '')
{
echo 'Description: <a class="fl">'.$description.'</a><br />';
}
// catégorie ODP
if (($categorie = $reponse['directoryCategory']['fullViewableName']) != '')
{
echo 'Catégorie: <a class="fl" href="http://directory.google.com/'.$categorie.'/">'.str_replace('/', ' > ', $categorie).'</a><br />';
}
// lien + cache
echo '<font color="#008000">'.($url = eregi_replace('^http://', '', $reponse['URL'])).' - '.$reponse['cachedSize'].'</font>';
echo ' - <a href="http://www.google.com/search?q=cache:'.urlencode($url).'" class="fl" target="_blank">En cache</a>';
// pages similaires
//if ($reponse['relatedInformationPresent'])
echo ' - <a href="api-google.php?q=related:'.urlencode($url).'" class="fl">Pages similaires</a>';
echo '<br /><br />';
}
// page prec
if ($resultats['startIndex'] > $maxResults)
echo '<a href="api-google.php?q='.urlencode($q).'&debut='.($resultats['startIndex']-$maxResults-1).'">«</a>';
// n° page courante
echo ' '.(int)(($resultats['startIndex'] / $maxResults)+1).' ';
// page suiv
if (($resultats['startIndex'] + $maxResults) == ($resultats['endIndex'] + 1))
echo '<a href="api-google.php?q='.urlencode($q).'&debut='.($resultats['endIndex']).'">»</a>';
}
}
else
{
// la requête n'a pas abouti
echo "Erreur !";
}
}
?>
</body>
</html>