Working with Google APIs to Fetch Results using ASP
by Vishal Parikh (a.k.a Graphiz)
Introduction
Google is experimenting with a free web service that can be used non-commercially to
gather search results in XML format. This format allows the search results to be
displayed in any way the user wishes or transformed into another format. The
interface is currently limited to returning only 10 results.
ASP Script
Below is an ASP script which has a very simple form which takes a input string from
user, queries the Google search engine and return first 10 results and displays the
results as simple links on the page. You can modify this script to perform other
tasks once the results are returned.
Code / Script
The following ASP script block creates the SOAP message that queries the Google API,
posts it to the Google web service (http://api.google.com/search/beta2) using its
search method (doGoogleSearch), places the result in an XML document and parses the
document, extracting the page title and URL of each item returned by the query. You
implement this by placing the script anywhere between the body tags of your ASP page
and modifying the line
Google_Web_APIs_license_key = "00000000000000000000000000000000000"
by changing the zeros to your actual license key (obtained from Google) and your
desired search string.
google.asp
<html>
<head>
<title>Google at its best... Graphiz Design </title>
</head>
<body>
<%
If Request.Form("SubmitButton") <> "Submit" Then
%>
<form method="post" name="g" action="google.asp">
<input type="text" name="searchstring" size="20"><br>
<input type="submit" value="Submit" name="SubmitButton">
<form>
<%
Else
%>
<p>
Results for Google Search:
<font color="red"><%=request.form("searchstring")%></font>
</p>
<%
Google_Web_APIs_license_key = "00000000000000000000000000000000"
If Request.Form("searchstring") = "" Then
Search_String = "Sample Search"
Else
Search_String = Request.Form("searchstring")
End If
'
' This SOAP message came from the Google API soap-examples.
' Dump the SOAP message into an XML document and set the key value,
' search string value and start index.
'
Set objInputXMLDoc = Server.CreateObject("Microsoft.XMLDOM")
objInputXMLDoc.load Server.MapPath("doGoogleSearch.xml")
objInputXMLDoc.selectSingleNode("//key").Text = Google_Web_APIs_license_key
objInputXMLDoc.selectSingleNode("//q").Text = Search_String
objInputXMLDoc.selectSingleNode("//start").Text = 0
'
' Post the SOAP message.
'
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objXMLHTTP.open "post", "http://api.google.com/search/beta2", False
objXMLHTTP.setRequestHeader "Content-Type", "text/xml"
objXMLHTTP.setRequestHeader "SOAPAction", "doGoogleSearch"
objXMLHTTP.send objInputXMLDoc
'showXML(objXMLHTTP.responseText)
'
' Dump the results into an XML document.
'
Set objOutputXMLDoc = Server.CreateObject("Microsoft.XMLDOM")
objOutputXMLDoc.loadXML objXMLHTTP.responseText
'
' Parse the XML document.
'
Set Nodes = objOutputXMLDoc.selectNodes("//item")
For Each Node In Nodes
Response.Write "<a href=""" & Node.selectSingleNode("URL").Text & """>" _
& Node.selectSingleNode("title").Text & "</a><br>" & VbCrLf
Next
End If
%>
</body>
</html>
<%
Function showXML(XMLSource)
Response.Clear
Response.Write XMLSource
Response.End
End Function
%>