Mega Code Archive

 
Categories / Delphi / Ide Indy
 

Using the Google web APIs with Delphi

Title: Using the Google web APIs with Delphi Question: How to access Google via WebServices Answer: Introduction, what are the Google Web APIs The Google Web APIs are api's that use WSDL and SOAP to access Google's search engine. It gives us the possibility to access the vast database of Google to do web searches. At the moment these API's are still in Beta. You are free to try them out if you have a license key and you don't do more than 1000 queries a day. The final version of the API will probably be very different from what it is now, but for experimenting with webservices it is actually great fun. You can download the Google Web APIs from this google site. After downloading the Google Web APIs you also have to make a Google Account to get the key you need to query Google. The source code of the client application we are going to build can be found here . If you unzip the file and try to run the program, it will not work without changing the MY_KEY constant in the file GoogleSearch.pas to the key you got in your mailbox from Google. Making a Google GUI Application We are going to make an Application that uses the Google Web APIs to search the web and shows the links so we can click them. To start with, we are going to make a simple Application by Choosing File - New Application. We first save the files. I save my project as GoogleGUI and my Main Form as form_main. I set the caption of the main form to Search Google and change the form name to frmMain. To make our application a little bit more modular I add a datamodule to the project, save it as data_google and name it dtmGoogle. On this datamodule I put an HTTPRio component and call it rioGoogle. This will be used to call the web service. Of course you can also put the HTTPRio component on the form itself, but I want to reuse the datamodule in another project later on to make a webclient. Now I need the GoogleSearch.wsdl file which describes the Google APIs. You can find this file in the zip file we downloaded from the google site. I first copy the file to our source directory. After that we fill in GoogleSearch.wsdl in the WSDLLocation property of our HTTPRio component. After filling in this property I can select the Service and Port properties from a drop down menu. I select GoogleSearchService and GoogleSearchPort. Our HTTPRio component is now ready to be used. Now I can import the WSDL file, so Delphi Interfaces will be created to call Google's Web Service. We do this by going to the File - New - Other and choosing WSDL Importer from the Web Services Tab. We first fill in the name of our WSDL file (GoogleSearch.wsdl). You can see that a few different interfaces , types and methods will be generated after clicking Finish Be sure to first download the Delphi 6.02 service pack from the Borland site. It contains a lot of important upgrades and even new functionality for web service applications. After importing GoogleSearch.wsdl we will have a GoogleSearch.pas file that we can use in our application. The most important method we are going to use is doGoogleSearch. We now go back to our dtmGoogle DataModule and create a new public Function in our TdtmGoogle Class called Search. This method has one parameter called text, and returns a GoogleSearchResult variable. We implement this method as follows. function TdtmGoogle.Search(Text: string): GoogleSearchResult; var originalSeparator : Char; begin originalSeparator := DecimalSeparator; try DecimalSeparator := '.'; Result := (rioGoogle as GoogleSearchPort). doGoogleSearch(MY_KEY,Text,0,10,False,'',False,'','UTF-8','UTF-8'); DecimalSeparator := originalSeparator; except DecimalSeparator := originalSeparator; raise; end; end; Normally the line of code written in blue should be enough, but because of an error in the Soap implementation of Borland Delphi, the program uses , instead of . for decimal seperators. If I only use the blue line, I get this error message if I call the function. You should also notice that doGoogleSearch has many different parameters I filled in. One of the more important ones is the first one where I have put in the constant MY_KEY. You can find this const defined in the GoogleSearch.pas file I modified. if you try out the example you can download here it will only work if you change this MY_KEY constants value to the key you got in your mailbox from Google. If you need more information about the different parameters of the GoogleSearch method you can look in the documentation that comes with the download. Now we have to add some functionality to our application so we can search google from our GUI interface. I add a TEdit, call it edtSearch, a TSpeedbutton called btnSearch. Now I add two event handlers. One OnCreate of the form, and one OnClick for our btnSearch. procedure TfrmMain.FormCreate(Sender: TObject); begin dtmGoogle := TdtmGoogle.Create(self); urlAction := TBrowseURL.Create(self); end; In this event handler I Create a TBrowseURL action and load the dtmGoogle datamodule. The TBrowseURL action is an action you normally can find in the ActionList. I only need this one action so I create it at runtime. I defined the urlAction variable in my Class declaration. type TfrmMain = class(TForm) edtSearch: TEdit; btnSearch: TSpeedButton; procedure btnSearchClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure GotoUrl(Sender : TObject); private urlAction : TBrowseURL; public { Public declarations } end; I now create the event handler for the onClick event handler procedure TfrmMain.btnSearchClick(Sender: TObject); var lcv : Integer; lbl : TLabel; SearchResult : GoogleSearchResult; begin SearchResult := dtmGoogle.Search(edtSearch.Text); for lcv := 0 to pred(Length(SearchResult.resultelements)) do begin lbl := TLabel.Create(self); lbl.Font.Color := clBlue; lbl.Font.Style := lbl.Font.Style + [fsUnderline]; lbl.Left := 12; lbl.Top := 45 + lcv * 21; lbl.Parent := Self; lbl.Caption := SearchResult.resultelements[lcv].URL; end; end; This code will create a label for each ResultElement of the SearchResult. If we run this code we will get the following result. If we again click the button we will create new labels, leaving the other labels were they were, so i add these lines of code to my event handler to remove the labels I created before. I also add an OnClick Event handler to the created labels. procedure TfrmMain.btnSearchClick(Sender: TObject); var lcv : Integer; lbl : TLabel; SearchResult : GoogleSearchResult; begin for lcv := pred(ComponentCount) downto 0 do begin if (Components[lcv] is TLabel) and (Components[lcv].Tag = 1) then Components[lcv].Free; end; SearchResult := dtmGoogle.Search(edtSearch.Text); for lcv := 0 to pred(Length(SearchResult.resultelements)) do begin lbl := TLabel.Create(self); lbl.Tag := 1; lbl.Font.Color := clBlue; lbl.Font.Style := lbl.Font.Style + [fsUnderline]; lbl.Left := 12; lbl.Top := 45 + lcv * 21; lbl.Parent := Self; lbl.Caption := SearchResult.resultelements[lcv].URL; lbl.OnClick := GotoUrl; end; end; This GotoURL event handler is implemented as follows. procedure TfrmMain.GotoUrl(Sender: TObject); begin urlAction.URL := (Sender as TLabel).Caption; urlAction.Execute; end; If we now again try out this program we will be able to click one of the blue labels and our default browser will automatically be started with the selected page. Our GUI client for Google is finished now. In my next article I will use our dtmGoogle datamodule to create a Web Application implemented as both an ISAPI DLL, a CGI application and an ASP Component. Conclusion Using Web Services and the Google API in particular is actually quite easy. The improved WSDL importer of Delphi 6.02 does it's job quite well and makes it possible to quickly add Web Service functionality to internet connected applications or as we will see in a following article, web applications. About the Author Bart Van den Poel is a consultant and trainer for PeopleWare nv in Belgium. If you've got any comments on this article you can write him a mail at bart_van_den_poel@peopleware.be. See the article here: http://users.pandora.be/rapporten/Articles/GoogleArticle/UsingGoogleAPIWithDelph.html