ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners


Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates














ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash



 Top ASP 101 Stories Top ASP 101 Stories
VBScript Classes: Part 1 of N
Migrating to ASP.NET
Getting Scripts to Run on a Schedule

QUICK TIP:
The QueryString Has a Maximum Length
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates


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
%>

Happy Programming
Prays for all

Graphiz

Related Articles


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers