1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| ?>
<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 = 'ABQIAA';
$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> |
Partager