Google API PHP-Script

;-) Hab mal eins gemacht.... am suchen... gefunden am extrahieren

Ich hoffe das funktioniert, ist alt und ich hab zusammenkopiert.

CODE
<?php
require_once("nusoap.php");

$key = ".......";
$start = 0;

$soapclient = new soapclient("http://api.google.com/search/beta2"); // [1]

$parameters = array(
"key" => $key,
"q" => $query,
"start" => $start,
"maxResults" => 10,
"filter" => false,
"restrict" => "",
"safeSearch" => false,
"lr" => "lang_de",
"ie" => "UTF-8",
"oe" => "UTF-8"
);
$result = $soapclient->call("doGoogleSearch", $parameters, "urn:GoogleSearch");
$searchtime = $result["searchTime"];
$begin = $start + 1;
$end = $start + $parameters["maxResults"];
$total = $result["estimatedTotalResultsCount"];


for ($i = 1; $i <= $parameters["maxResults"]; $i++)
{
$element = $result[$i];
$url = $element["URL"];
$title = $element["title"];
$snippet = $element["snippet"]; // [2]



echo <a href="'.$url.'">'.$url.'</a><br>';
}

?>


Needed File nusoap.php


QUOTE
/*

NuSOAP - Web Services Toolkit for PHP

Copyright © 2002 NuSphere Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

If you have any questions or comments, please email:

Dietrich Ayala
dietrich@ganx4.com
http://dietrich.ganx4.com/nusoap

NuSphere Corporation
http://www.nusphere.com

*/
 
Zum Google-API ist das Buch "Google Hacks" zu empfehlen. Da gibts viele Anleitungen und Code-Schnipsel drin. Plus noch ein Kapitel für Webmaster.

Gruss, Remo
 
Ich habe das Buch gerade nicht zur Hand, aber ich denke es waren weder PHP noch ASP-Skripte drin, sondern eher Perl.
 
Schau mal auf Digital Point Solutions
biggrin.gif
 
Moin!
QUOTE Ich hoffe das funktioniert, ist alt und ich hab zusammenkopiert.

Das tut es nicht.

Da ein mehrdimensionaler Array zurückgegeben wird, müsste der Zugriff wie folgt aussehen:


CODE
...
$searchtime = $result["return"]["searchTime"];
$begin = $start + 1;
$end = $start + $parameters["maxResults"];
$total = $result["return"]["estimatedTotalResultsCount"];


for ($i = 1; $i <= $parameters["maxResults"]; $i++)
{
$element = $result["return"]["resultElements"][$i];

...






Das was ich da jetzt rausbekomme ist nicht gerankt.
Die ersten drei Ergebnisse sind teilweise die gleiche Seite.

Kann man irgendwie an gerankte Ergebnisse rankommen?
Bei der webmasterforum-Suche müsste doch das gleiche Problem bestanden haben, oder?

Gruß,
Ansgar
 
;-) Hast recht, ich habs aus der Funktion kopiert, die in der Schlaufe aufgerufen wird, wenigstens jemand der es auch ausprobiert hat ;-)

Es ist eigentlich schon geranked. Nur nicht fertig und es besteht ein Problem mit Duplicate Content in den Google API Suchresultaten. Siehe http://www.webmasterforum.ch/topic-2137.html

CODE for ($i = 0; $i < count($result)-1; $i++) {
$element = $result[$i];
$url = $element["URL"];
$title = $element["title"];
$snippet = $element["snippet"]; // [2]

//dublicate content:
if ( (!$known[$url] and !$known[$title]) ) {
$known[$title] = true;
$known[$url] = true;


if ($title != "\n" && $url != "\n") {
$tmpl = $this->res_tmpl;
echo utf8_decode(str_replace("{url}", $url,
str_replace("{snipplet}",
str_replace("<br>", "", $snippet),
str_replace("{title}", $title, $tmpl)
)
)
);
// ncrement no_of_res

$this->no_of_res++;
}
}
}
 
QUOTE
Es ist eigentlich schon geranked. Nur nicht fertig und es besteht ein Problem mit Duplicate Content in den Google API Suchresultaten. Siehe http://www.webmasterforum.ch/topic-2137.html


Ok, den kannte ich. Das mit den doppelten Einträgen ging nicht direkt daraus hervor.

Aber jetzt weiß ich, dass es normal ist und nicht auf einem Fehler von mir beruht.

Danke für den Code-Schnipsel.

Gruß,
Ansgar
 
Hallo,

Ich hab mir eure einzelnen Teile mal zusammengebastelt, aber ein Ergebniss bekomm ich nicht??
Nur ne weisse Seite?

Könnt mir jemand das komplette Script mailen?

Danke

Chris

 
QUOTE Ich hab mir eure einzelnen Teile mal zusammengebastelt, aber ein Ergebniss bekomm ich nicht??


Analysier mit den Funktionen print_r oder var_dump den Array, den du vom Google-Server zurückbekommen hast.
Vermutlich hast du auf nicht exisitierende Felder im Array zugegriffen.
 
Eine dumme Frage: (auch wenn es die nicht geben soll)

Wie übergebe ich das gesuchte Wort an das Skript?
 
CODE $parameters = array(
"key" => $key,
"q" => $query,


D.h. der Wert muss vorher in $query, wenn er z.B. aus einem Formular kommt $query = $_REQUEST['query']; über dem restlichen Code.
 
Danke Alain,
habe den Code angepasst.
Aber es geht immer noch nicht. Bekomme im Quelltext des Browser folgendes angezeigt.
CODE
<p>query</p><hr />test<a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br><a href=></a><br>




CODE
<?php
if (isset($_POST["query"])) {
echo "<p>query</p>";
echo "<hr />";
require_once("nusoap.php");

$key = ".......";
$start = 0;

$query = $_POST['query'];

echo "$query";

$soapclient = new soapclient("http://api.google.com/search/beta2"); // [1]

$parameters = array(
"key" => $key,
"q" => "$query",
....
else {
echo "<p>query nicht übergeben</p>";
?>
<form action="<?php echo $_SERVER[´PHP_SELF´]; ?>" method="post">
<input type="text" name="query" />
<input type="submit" />
</form>
<?php
}

?>

 
Zurück
Oben