Home: PHP Diary | Script School | PHP Scripts | TD Scripts.com
TD Forum - An unthreaded messageboard script to exchange ideas with your site visitors


[back]
go back 01/12/03 "Automated Google queries & API, include_once, require_once" go forward[next]

Where to find Google API tools

The Google developer API and at the time of this writing is available here: http://www.google.com/apis/ With this API developers and license key entitle each account holder (only 1 account per developer) to 1,000 automated queries per day. 1000 automated queries a day is actually quite a few. Again, at the time of this diary entry it is a beta service, so it's uncertain if this will be a permanent service.

I downloaded the API to see what I might be able to do with it. It uses WDSL so I next went to the php manual to see what type of WDSL support exists within PHP. My searches there came up empty, so I was in search of someone else that had written a SOAP server/client. Indeed, there were several like this one: http://dietrich.ganx4.com/nusoap/ 

require_once / include_once

I've noticed more and more code lately including these built-in php function calls so I consulted the PHP manual for more information on what these functions do (yes, I realize they sound pretty self-explanatory)

include_once - http://www.php.net/manual/en/function.include-once.php - will only include the file one time in cases where it is possible to have multiple includes of the same file this would be useful. I didn't do a benchmark to check versus using the include, but it would be interesting to test which one was faster. My guess (and it's only a guess) is include is faster because no additional checks would be necessary. Therefore, this function is probably only useful for larger programs.

require_once - http://www.php.net/manual/en/function.require-once.php - from my understanding, require functions are processed before the includes (which might be optionally included), so using my comments on include_once above, you can see where having this function in use could be handy. Again, my guess would be that require_once is not as efficient as require. Useful in larger programs.

Ok, so how to actually use this Google WDSL file to query Google's database?

STEP 1. Have you signed up for the Google APIs developer program? You need to do so in order to get a $key to use in your queries. You are limited to a maximum of 1000 automated queries per day. Sign up at: http://www.google.com/apis/ by following the link that says: "Create Account"

STEP 2. Download the Google APIS. The download URL is at the instructions page in STEP 1. Be sure that you FTP the following WDSL file (in ASCII mode) to the same place as your SOAP server/client: googlesearch.wsdl 

STEP 3. Download (or write your own) SOAP server / client and install on your webserver. If you haven't written your own, or don't know how to, then use your favorite search engine to find one written by a third party and/or use the one I tried out in this diary entry.

STEP 4. Formulate your $queryterms. Ok, well, what do you want to query? What keywords or keyword are you interested in getting the results from?

<?
include('/path/to/SOAPCLIENT.php'); 
$key = 'here_goes_your_google_account_key';
$queryterms = 'TDavid'; // these are the keyword(s) you want to query
$query = array( 'key' => $key,
                         'q' => $queryterms, 
                         'start' => 0, 
                         'maxResults' => 10, 
                         'filter' => true, 
                         'restrict' => '', 
                         'safeSearch' => false, 
                         'lr' => '', 
                         'ie' => '', 
                         'oe' => '' ); 
$soapclient = new soapclient('googlesearch.wsdl', true);
$result = $soapclient->call('doGoogleSearch',$query);
$i = 1;
print("Top 10 results for <b>$queryterms</b><br><br>");
foreach ($result as $key => $value) {
 if($key == 'resultElements') {
   foreach ($value as $key2 => $value2) {
     foreach($value2 as $key3 => $value3) { 
       if($key3 == 'URL') {
        $snippet = substr(strip_tags($value2["snippet"]), 0, 70);
        $summary = substr($value2["snippet"], 0, 70);
        $title = substr(strip_tags($value2["title"]), 0, 70);
        $cachedSize = $value2["cachedSize"];
print("#$i: <b><a href=\"$value3\">$title</a></b><br>Summary: $summary<br><font color='green'>$value3 - $cachedSize</font><hr>");
$i++;
     }
     //print("$key3 $value3<br>");
     }
   }
 }
//print("Key: $key; Value: $value<br>\n");
}
?>

Uncomment the print statements to see the various array indices available to you or consult the reference page in the Google API.

Here are the first 5 Google results that this script returns as of the time of writing this diary entry. Check the query out in Google for yourself by clicking here

#1: TD Scripts.com - Perl CGI PHP Scripts, mySQL, Tutorials, and help ...
Summary: ... Click here to learn more about TDavid8/3 - 8/5/01:
http://www.tdscripts.com/index.shtml - 32k


#2: About TDavid
Summary: TDavid started programming using Basic back in the Vic-20 and C
http://www.tdscripts.com/bio.html - 7k
#3: Adult NetSurprise
Summary: User Name: TDavid. Email Address: Birthday: September 29. ICQ N
http://www.adultnetsurprise.com/view-webmaster/7912/ - 12k
#4: php scripts - learn php programming and download original php ...
Summary: ... CHAT with TDavid Script School Chat. ... 11/
http://www.php-scripts.com/ - 24k
#5: Learn PHP with TDavid
Summary: ... Ok, I'm nervous...let's see if it works, because I see som
http://www.php-scripts.com/php_diary/121899.html - 10k

Neat stuff! Just remember that you are only allowed 1000 automated queries per day and you cannot have multiple accounts.

TiVO CES announcement

TiVO is probably one of the best technical innovations in recent memory for the average Joe. They announced at the Consumer Electronics Show in Vegas they will be unveiling a new Home Media Option: http://www.tivo.com/5.3.1.1.asp?article=162

Happy coding to you!

Please vote on the usefulness of this diary entry so other people will know if it is worth their time to read :)

How useful was this diary entry? Avg Surfer Rating: 3.89 (37)

New forum for discussion of diary entries

The homeroom at Script School is available to discuss this and other php-scripts.com diary entries. You must be an enrolled student at Script School to add comments to these diary entries.

[back]go back 01/12/03 "Automated Google queries & API, include_once, require_once" go forward[next]

Home: PHP Diary | Script School | PHP Scripts | TD Scripts.com

Copyright 1999-2003 php-scripts.com Last Modified 01/13/03 09:47