Click here to Skip to main content
15,890,438 members
Articles / Programming Languages / ASP
Article

How to use the Google API in ASP

Rate me:
Please Sign up or sign in to vote.
3.50/5 (6 votes)
4 Jul 20031 min read 92.9K   47   3
This shows you how to use the Google API in ASP not ASP.NET!

Sample image

Introduction

I have found how to use the Google API in ASP! Before you use these functions, please check out http://api.google.com/. In order to use these examples, you need a key which will limit you to 1000 queries a day. This is available free from the API website. The Google API uses SOAP to allow data transfer between two different OSs easily. These examples use Microsoft's SOAP Toolkit to manage the connection to the remote server (in this case Google). Above is a screenshot of it in action on my website. Here is a fully working example of the search function. To use the API, call these functions in your ASP file where you want it to appear, and specify the necessary parameters.

To search Google, use this function:

VBScript
GoogleSearch(Query, Start, MaxResults, Filter, Restrict, SafeSurf, Lr)
  Dim Key
  Key = "your key goes here"

  Dim SoapClient 
  set SoapClient = Server.CreateObject("MSSOAP.SoapClient")
 
  Dim NodeList    
  SoapClient.ClientProperty("ServerHTTPRequest") = True
  SoapClient.mssoapinit "http://api.google.com/GoogleSearch.wsdl"
  Set NodeList = SoapClient.doGoogleSearch(key, _
      Query, Start, MaxResults, Filter, 
      Restrict, SafeSurf, Lr, "", "")
  
  Set NodeList = Nothing
  Set SoapClient = Nothing
End Function

This will give you an array that you will have to parse, to get the useful information out. Here is a function which will format the results in a similar format to Google's own results:

VBScript
Function DisplayList(ByRef ResultNodeList)
   ResultNodeList.Reset
   Dim documentFiltering, estimatedTotalResultsCount
   Dim searchTime, endIndex, searchTips, searchComments
   Dim startindex, estimateIsExact, searchQuery 
   Dim yIndex, tempDir, CategoryHTML, DocSize
   Dim DocSnippet, DocCat, DocRelated, DocCatTitle
   Dim DocCatDesc, DocURL, DocTitle, iIndex, HTMLOutput

   documentFiltering = ResultNodeList.Item(1).Text
   estimatedTotalResultsCount = ResultNodeList.Item(3).Text
   searchTime = ResultNodeList.Item(7).Text
   endIndex = ResultNodeList.Item(11).Text
   searchTips = ResultNodeList.Item(13).Text
   searchComments = ResultNodeList.Item(15).Text
   startindex = ResultNodeList.Item(17).Text
   estimateIsExact = ResultNodeList.Item(19).Text
   searchQuery = ResultNodeList.Item(21).Text
    
   CategoryHTML = "<H3>Results " & startindex & " - " & endIndex & " of 
      about " & estimatedTotalResultsCount & ". Search took " & searchTime 
      & " seconds.</H3>"
    
   For yIndex = 0 To 
      ResultNodeList.Item(5).childNodes.length - 1

      If ResultNodeList.Item(5).childNodes.Item(yIndex).nodeName _
                                                        = "item" Then
         tempDir = ResultNodeList.Item(5).childNodes.Item(yIndex).Text
         CategoryHTML = CategoryHTML & _
               "<a href=""http://directory.google.com/" & _
               tempDir & "/"">" & DirTree(tempDir) & _
               "</a><br>"
         Response.Write CategoryHTML
      End If
   Next
    
   For iIndex = 0 To 
    ResultNodeList.Item(9).childNodes.length - 1
    If ResultNodeList.Item(9).childNodes.Item(iIndex).nodeName = "item" Then
      
     DocSize = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(1).Text
     DocSnippet = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(5).Text
     DocCat = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(7).Text
     DocRelated = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(9).Text
     DocCatTitle = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(11).Text
     DocCatDesc = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(13).Text
     DocURL = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(15).Text
     DocTitle = _
      ResultNodeList.Item(9).childNodes.Item(iIndex).childNodes.Item(17).Text
     DocCat = RemoveLB(DocCat)        

     If DocTitle = "" Then
       DocTitle = "Untitled"
     End If
        
     If DocSnippet <> "" Then
       DocSnippet = DocSnippet & "<br>"
     End If
        
     If DocCatDesc <> "" Then
       DocCatDesc = "<font color=gray class=sm>Description:</font> " _
         & DocCatDesc & "<br>"
     End If
         
     If DocCat <> "" Then
       DocCat = "<font color=gray class=sm>Category:" & _
         " <a href=""http://directory.google.com/" & DocCat & _
         "/"">" & DirTree(DocCat) & "</a></font><br>"
     End If
            
     HTMLOutput = HTMLOutput & _
       "<a href=""" & DocURL & """>" & DocTitle & "</a><br>" & _
       DocSnippet & DocCatDesc & DocCat & _
       "<font color=green>" & DocURL & " - " & DocSize & _
       "</font><br><br>"
    End If
   Next
   Response.Write HTMLOutput
End Function

Function DirTree(tempDir)
  Dim tempDirTree, RemoveLB
  tempDirTree = Replace(tempDir, "Top/", "")
  tempDirTree = Replace(tempDirTree, "/", " > ")
  tempDirTree = Replace(tempDirTree, "_", " ")
  DirTree = tempDirTree
End Function

Function RemoveLB(in_str)
  in_str = Replace(in_str, Chr(10), "")
  RemoveLB = in_str
End Function

You would call this with DisplayList NodeList

The Google API also provides a Spell Checker service. Here is the function to use it.

VBScript
Function PerformGoogleSpellingSuggestion(Words)
  Dim Key
  Key = "your key goes here"
  
  Dim SoapClient
  set SoapClient = Server.CreateObject("MSSOAP.SoapClient")
  
  Dim RetVal
  SoapClient.ClientProperty("ServerHTTPRequest") = True
  SoapClient.mssoapinit "http://api.google.com/GoogleSearch.wsdl"
  RetVal = SoapClient.doSpellingSuggestion(Key, Words)

  If RetVal > "" Then
    Response.write RetVal
  Else
    Response.Write "No Suggestions"
  End If
  
  Set SoapClient = Nothing
End Function

There is also Google's Cached Page function which gives you a 'snapshot' of the page when Google visited it. Here it is:

VBScript
Function PerformGoogleGetCachedPage(Url)
  Dim Key
  Key = "your key goes here"
  
  Dim SoapClient
  Set SoapClient = Server.CreateObject("MSSOAP.SoapClient")
  
  Dim RetVal
  Dim DecodedPage
  Dim i
  dim NewArray
  
  SoapClient.ClientProperty("ServerHTTPRequest") = True
  SoapClient.mssoapinit "http://api.google.com/GoogleSearch.wsdl"
  RetVal = SoapClient.doGetCachedPage(Key, Url)
  NewArray = RetVal
  
  for i = 1 to ubound(NewArray)
    DecodedPage = DecodedPage & chr(ascB(MidB(NewArray, i, 1)))
  next
  
  Response.Write DecodedPage
  Set SoapClient = Nothing
End Function

So there are all the functions available to you to use on your web page. These services are completely free and are provided by Google. Beware of the 1000 a day limit!

Checkout my website.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Australia Australia
Nick is a high school student in grade 10. He started mucking around with BASIC at the age of 8. He is fluent in VB, ASP, BASIC, C++, JavaScript and is learning .NET

Comments and Discussions

 
GeneralGoogle Parser online tool (GooParser) Pin
jp73129-Oct-07 17:31
jp73129-Oct-07 17:31 
GeneralLegal issue Pin
Jia.Chen22-Aug-07 11:05
professionalJia.Chen22-Aug-07 11:05 
GeneralSpell Check Pin
ZenFaz18-Jun-06 21:25
ZenFaz18-Jun-06 21:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.