Error » Search Engines » Google » Google APIs » Google Web API Tutorial

Post New Thread Reply
  Google Web API Tutorial
LinkBack Thread Tools Display Modes
Old 29-Dec-2006, 12:34 AM   #1 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Google Web API Tutorial

What is the Google Web API?

The API* exposes the Google engine to developers. This means you can write scripts that access the Google search in real-time. For example, you could query all pages containing "hello", and output the page-count. Or anything else imaginable.
Limitations

You have a default limit of 1,000 queries per day, you cannot get over the 1,000th result, you can only query for 10 results a time, and you can only access Google Web Search (not Google Images, Google Groups, and so on).
* Depending on the source, the abbreviation "API" stands for Application Programming Interface, Application Programmer's Interface, Application Programmer Interface, Application Program Interface, or Application Programmatic Interface. In short, it's a wrapper around a program that helps developer's to easily access its inner functionality. The Google Web API is also called "Google Web Services".
Do I have what it takes?

What you need is a server that allows you to execute server-side scripts. If you got www.example.com, you should check your contract and wether or not you can do that; email your host support if in doubt. Ask them if they have CGI, scripting like PHP, Pythôn, Java, ASP, or the like.
Second, you need to know the programming basics. But using the Google Web API is not very complicated, as there are several wrapping tools to get you started.
For this small tutorial, I will give examples of a PHP implementation. You can use other languages as well (like Perl). You should be familiar with HTML as well. PHP is a language, besides other things, to dynamically create HTML pages.
The Google API (currently in Beta) is further based on the Web Services technology SOAP (the XML-based Simple Object Access Protocol). But you don't need to details of SOAP to use the API, really.
Downloading the API, and registering for a key.

Create your own folder on your computer and put all the needed files in it; first, you should download the toolset of the Google Web API (note that you don't need it for this sample), and get your developer key (which you do need for the sample):Second, get the helpful file called "NUSOAP" by Dietrich Ayala. It's free and helps you to implement a Google Web API script in PHP. (Note that I put NUSOAP on my server because I made a small but important change to the file which was necessary to get the filter-parameter to work.)
Now that everything's unpacked and waiting in your folder, create your own test project by creating a sub-folder called "test". Put the "nusoap.php" file in the folder.
Next, use Notepad – or another text-editor, like my Netpadd – and write the following testing PHP script as "index.php" and upload it to your server via FTP (you can use Absolute FTP for that; it's free to test for 30 days):
<?
require_once("nusoap.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "DTD/xhtml1-strict.dtd"><html
xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<title>Test</title>
</head>
<body>

<h1>Hello World</h1>

<?
$Query = "Hello World"; // [3]
$key = "[Put your developer's key here]";
$start = 0;

$parameters = array(
"key" => $key,
"q" => $Query,
"start" => $start,
"maxResults" => 10,
"filter" => true,
"restrict" => "",
"safeSearch" => false,
"lr" => "lang_en",
"ie" => "",
"oe" => ""
);

$soapclient = new soapclient("http://api.google.com/search/beta2"); // [1]
$result = $soapclient->call("doGoogleSearch", $parameters, "urn:GoogleSearch");
$searchtime = $result["searchTime"];
$begin = $start + 1;
$end = $start + $parameters["maxResults"];
$total = $result["estimatedTotalResultsCount"];

if ($total > 0)
{
$result = $result["resultElements"];

for ($i = 0; $i < $parameters["maxResults"]; $i++)
{
$element = $result[$i];
$url = $element["URL"];
$title = $element["title"];
$snippet = $element["snippet"]; // [2]
?><p><?= $snippet ?></p><?
}

}
?>

</body>
</html> The above is a mix of HTML and PHP, separated by the "<?" and "?>" strings. What it does is instantiate a Google Web API search object called "soapclient" (see highlight [1]), and output all the snippets of the search result (see highlight [2]). Note that for this to work you must insert your own developer's key!
If you uploaded the file and entered the URL in a web-browser (like "www.example.com/test/"), you should see several paragraphs with Google snippets. Those are grabbed dynamically and will be different if you change the query "Hello World" (highlight [3]) in above PHP script.
Also, you can see above script in action, or download the complete project source for your own use.
What to do next?

Now that you got the basics to work, I suggest to play around with the script and be creative with it. Try out new things, like outputting the title, URL, and search-time as well. If you got a website indexed by Google, you can also build your own search engine that looks exactly like you want. You can also look at some of the tools at this site for inspiration (see navigation, like Egobot chat), or buy the Google Hacks book and read through many more samples.
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -8. The time now is 05:00 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228