Google API with Services_Google

Uncategorized — Titus Barik on October 24, 2004 at 10:19 pm

Added Services_Google support from PEAR to the weblog, a class that is essentially a SOAP wrapper to the Google API. The package is ultimately simple to setup, but the lack of documentation, the use of iterators, and the PHP SOAP extension requirement make this class a little cumbersome to utilize. Still, it gets the job done. Here’s how I use it.

First, I instantiate the Services_Google class and set the Google key, which allows me a maximum of one thousand queries per day:

$g = new Services_Google($key);

Next, I execute the search that I want, where I limit the search to my website:

$g->search("$query site:www.barik.net");

Finally, I utilitize the iterator functionality of PHP 5 on the object:

foreach ($g as $key => $result) {
  if ($result->URL == '') return;
  echo $result->snippet;
  echo $result->title;
}

You get a result for each document that Google returned. You can use print_r on $result to see all the data that each record contains. Finally, the Services_Google class has a bug that causes it loop more times than records. Thus, I do a small hack and forcefully return from the loop when one of the records has an empty URL. That’s really all there is to it.

3 Comments »

  1. You need to start applying to grad school, so you can apply yourself at something else. Soon, with all this free time, you will be unable to talk to another person without the words "compile" or "dynamically link" in the conversation. And with these things called "girls," "compile" and "dynamically link" are big no no’s.

    Comment by Juan — October 25, 2004 @ 8:40 pm
  2. I just checked your wiki, and noticed that I’m not on your list of friends. I see how it is!

    Comment by Juan — October 25, 2004 @ 8:44 pm
  3. That’s because it’s a wiki. You are supposed to edit the page and add yourself.

    Comment by Titus Barik — October 25, 2004 @ 10:55 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WP Hashcash

titus@barik.net | The Weblog of Titus Barik