Sie sind auf Seite 1von 18

Chapter 2: Web Services Implementation Overview In an effort to do a further research on Web Services, I have developed a small client interface

to Google API. This is an exploratory case study motivated by one particular observation. I have been fascinated by the current pace of progress and innovation of application developments that utilize Web API provided by Google, eBay, Yahoo and Amazon. For example, entities such as Amazon and eBay allow external partners and associates to plug into their own IT infrastructure through Web services. HousingMaps.com is another application that utilizes Google Maps API with Craigslist to generate rental listings with an added functionality of Google maps. I wanted to experiment further into Google API with one question whether it is possible to test the service with two distinct programming languages and produce the same result. This experiment starts with the premise that Web services promote decoupling and language independence. Without knowing their

deploying platform, a seamless implementation of a Web service client to Google Web service can validate the hypothesis. The results presented in this experiment makes use of a large set of documents related to Google API Google API documentation, public

commentaries in form of tutorials, web sites, public articles, and blogs.

Author Denise M. Gosnell of Professional Development with Web APIs: Google, eBay, Amazon.com, MapPoint, FedEx describes Web APIs as application programming interfaces that are available over the Internet. In short, Web API can be regarded as a series of Web services -- each of which has one or more procedures that can be called using the Internet. By using the Simple Object Access Protocol (SOAP) and Web Services Definition Language (WSDL) specifications, Google lets developers choose their favorite development environment -- including (but not limited to) .NET, C/C++, Java, PHP, Perl, Python -- on which applications can be written to consume its online search engine functions as a Web service. In other words, anybody can use Google Web APIs to query its search index directly from their own computer programs. Two parallel experimentations with Google Search API have been done to demonstrate that developers can choose any development

environment on which application can be written to use Google search engine functions as a Web Service. The first implementation of Google Client is applied in a J2EE environment and the second one is done in PHP. A successful experimentation done through the use of two different development environments (Java and PHP) will hence validate the theory of loose coupling of Web services.

Thus, the purpose of this experiment is to use Google API and show how it can be used as a part of Web Service to set up a client interface into its database without tightly integrating the two systems. As mentioned, Google Web services provide a mechanism for a client program, executing on a local host, to invoke a server function such as Google Search, Google Spellings and Google Cached Page. The search result is gathered in XML format which allows us to present the result in myriad ways or transform it into another format.

The following illustration shows how client for Web Services can interact with Google Web API.

http://www.devarticles.com/index2.php? option=content&task=view&id=422&pop=1&page=0&hide_js=1

Callable functions Search - search for specified term in the Google database

Corresponding methods in GoogleSearchProxy doGoogleSearch

Cache - retrieve cache page from the Google doGetCachedPage cache Spelling - retrieve a spelling suggestion from doSpellingSuggestion Google Table: Summary of functions that can be performed with Google API Web Services

The above methods exposed by the Googles Web Services make it possible to use number of features Web search, cached document retrieval, phrase correction in a simple but extensible manner, which opens up very interesting new possibilities for Web developers.

Java Implementation: In order to access Google Web service from a Java client, I used Web Sphere Studio that can generate a client proxy from the WSDL file provided by Google. The client proxy is a Java class that has a method for each operation defined in the WSDL file. This proxy is used by client code to make a Web service invocation. It uses the Apache SOAP runtime component to marshal the Java method arguments into XML and un-marshal the XML response into the Java return value. For example, this proxy is used to invoke the doGoogleSearch service. The invocation is done using the Simple Object Access Protocol (SOAP). The client proxy gives the client program a convenient way to invoke

the remote server function. The proxy and skeleton hide the protocol details from the local client program and the remote server function implementation.

Components Utilized

Windows XP WebSphere Studio Application Developer, Version 5 Google developer kit : googleapi.zip The GoogleSearch.wsdl - WSDL file that describes technical

information needed to access and consume the Web services provided by Google. Assumption

There is a direct network connection to reach the Google server

Experimentation Progresses in following steps: 1. Preparation i. ii. iii. Install IBM Websphere Studio Install Google Developer Kit Get Google License Key

2. Implementation i. ii. Import Google WSDL Write Java Proxy classes

iii.

Run the program

Google Search Web deployment descriptor

Fig: The sample application has a methods pane that lists all the operations in the service and allows us to select a service to test. Here, we have selected the doGoogleSearch method and entered SFSU College of Business as the search query. The result pane shows the output of the operation. Once all of the parameters are entered, the Java Client invokes a doGoogleSearch service request to the Google API. Google Web Services in turn, returns the result in XML format. A SOAP XML message, for example, from Google Web Services for a search request might look like:

<?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:doGoogleSearch xmlns:ns1="urn:GoogleSearch" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <key xsi:type="xsd:string"> WFJkmtVQFHKAh1mVOWQYZquxDPt6VmMr </key> <q xsi:type="xsd:string">SFSU College of Business</q> <start xsi:type="xsd:int">0</start> <maxResults xsi:type="xsd:int">10</maxResults> <filter xsi:type="xsd:boolean">true</filter> <restrict xsi:type="xsd:string"></restrict> <safeSearch xsi:type="xsd:boolean"></safeSearch> <lr xsi:type="xsd:string"></lr> <ie xsi:type="xsd:string"></ie> <oe xsi:type="xsd:string"></oe> </ns1:doGoogleSearch> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

doGoogleSearch.xml The parameters required to invoke a doGoogleSearch service is listed in the following table. It lists all the valid name-value pairs that can be used in a search request and describes how these parameters will modify the search results.
key q start Provided by Google, this is required for you to access the Google service. Google uses the key for authentication and logging. Search Query Zero-based index of the first desired result.

maxResul Number of results desired per query. ts filter Activates or deactivates automatic results filtering, which hides very similar results and results that all come from the same Web host. Restricts the search to a subset of the Google Web index, such as a country like "Ukraine" or a topic like "Linux."

restrict

safeSearc A Boolean value which enables filtering of adult content in the h search results. lr Language Restrict - Restricts the search to documents within one or more languages.

ie

Input Encoding - this parameter has been deprecated and is ignored. All requests to the APIs should be made with UTF-8 encoding. Output Encoding - this parameter has been deprecated and is ignored. All requests to the APIs should be made with UTF-8 encoding.

oe

Fig: shows the results of above query for SFSU College of Business.

Fig:

The following snippets represent part of the Google web services WSDL structure. It describes the XSD schema for GoogleSearchResult and ResultElement types.
<xsd:complexType name="GoogleSearchResult"> <xsd:all> <xsd:element name="documentFiltering" type="xsd:boolean"/> <xsd:element name="searchComments" type="xsd:string"/> <xsd:element name="estimatedTotalResultsCount" type="xsd:int"/> <xsd:element name="estimateIsExact" type="xsd:boolean"/> <xsd:element name="resultElements" type="typens:ResultElementArray"/> <xsd:element name="searchQuery" type="xsd:string"/> <xsd:element name="startIndex" type="xsd:int"/> <xsd:element name="endIndex" type="xsd:int"/> <xsd:element name="searchTips" type="xsd:string"/> <xsd:element name="directoryCategories" type="typens:DirectoryCategoryArray"/> <xsd:element name="searchTime" type="xsd:double"/> </xsd:all> </xsd:complexType> <xsd:complexType name="ResultElement"> <xsd:all> <xsd:element name="summary" type="xsd:string"/> <xsd:element name="URL" type="xsd:string"/> <xsd:element name="snippet" type="xsd:string"/> <xsd:element name="title" type="xsd:string"/> <xsd:element name="cachedSize" type="xsd:string"/> <xsd:element name="relatedInformationPresent" type="xsd:boolean"/> <xsd:element name="hostName" type="xsd:string"/> <xsd:element name="directoryCategory" type="typens:DirectoryCategory"/> <xsd:element name="directoryTitle" type="xsd:string"/> </xsd:all> </xsd:complexType>

Similarly, a call to retrieve a spelling suggestion from Google, doSpellingSuggestion is shown in the following figure. In this example, when an incorrect word dictionari is sent to the Google API along with Google key, it replies back with the corrected spelling dictionary.

Fig:

PHP Implementation: Program Definition: This section will provide a summary of how I have used PHP programming language to create an online interface for Googles search called Google Search. I chose PHP for my other client interface development of Google Search API because of its usefulness in

rapid application development. In addition, it would expand my knowledge on PHP as well. A full demo of PHP implementation can be tested at http://cob.sfsu.edu/temp/ . It basically is a web interface to tap into Googles index and search for specific terms. It successfully submits and receives requests to and from Google API, allowing us to display the search result on our Web site in a customized way for a different web presentation. The Google Search has following basic features: i. ii. iii. iv. v. vi. vii. Spell Check Find Related Pages Search within a particular site (e.g.: sfsu.edu) Find/View Cached Page Convert HTML file to PDF format Get Google Rank for a particular site Get Google suggestion for search queries

Components Utilized Apache Web Server PHP 5 GoogleSearch.wsdl NuSOAP HTML, Cascading Style Sheet

o o o o o

Experimentation Progresses in following steps: 1. Preparation i. Install NuSOAP

ii.

Get Google License Key

2. Implementation i. ii. iii. Create a SOAP client Transmit the SOAP request Receive and extract data from the response

Create a SOAP client in PHP To create the SOAP client, the program takes advantage of the built-in SOAP functionality of PHP. Following part of the code generates a new client and stores the result in variable called soapclient. $soapclient = new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl');

Next, it is time to send the request to the SOAP server. Now that the client has been created, SOAP parameters are initialized and call to one of the Google Service function doGoogleSearch is done.

$soapclient = new soapclient('http://api.google.com/GoogleSearch.wsdl', 'wsdl'); $parameters = array( 'key'=> $>key, 'q' => $query, 'start' => $start,

'maxResults' => $results, 'filter' => $filter, 'restrict' => '', 'safeSearch' => $safe, 'lr' => '', 'ie' => 'latin', 'oe' => 'latin' $results = $soapclient->call('doGoogleSearch',$parameters);

The above code sends the SOAP request to the Google services specified in the WSDL file. As a result, Google SOAP response will be generated.

Transmit the SOAP request Here is the Google SOAP API's response message in XML format for doGoogleSearch

HTTP/1.1 200 OK Date: Thu, 18 Nov 2005 12:46:08 GMT Content-Length: 1325 Content-Type: text/xml; charset=utf-8 <?xml version='1.0' encoding='UTF-8'?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <ns1:doGoogleSearchResponse xmlns:ns1="urn:GoogleSearch" SOAPENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <return xsi:type="ns1:GoogleSearchResult"> <documentFiltering xsi:type="xsd:boolean">false</documentFiltering> <estimatedTotalResultsCount xsi:type="xsd:int">276</estimatedTotalResultsCount> <directoryCategories

xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Array" ns2:arrayType="ns1:DirectoryCategory[0]"> </directoryCategories> <searchTime xsi:type="xsd:double">0.071573</searchTime> <resultElements xmlns:ns3="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns3:Array" ns3:arrayType="ns1:ResultElement[0]"> </resultElements> <endIndex xsi:type="xsd:int">0</endIndex> <searchTips xsi:type="xsd:string"></searchTips> <searchComments xsi:type="xsd:string"></searchComments> <startIndex xsi:type="xsd:int">0</startIndex> <estimateIsExact xsi:type="xsd:boolean">false</estimateIsExact> <searchQuery xsi:type="xsd:string">SFSU College of Business</searchQuery> </return> </ns1:doGoogleSearchResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Receive and extract data from the response Subsequent to SOAP response from Google services, it is now time to receive and extract data from the response in PHP. Following code listing captures the response is in PHP.

$amount = $results['estimatedTotalResultsCount']; $title = $result['title']; $snippet = $result['snippet']; $url = $result['URL'];

Conclusion: This small experiment proved that it is possible to have flexible connections among applications running on diverse platforms. Without knowing the intricate details of Googles database systems or its platform, the experiment illustrates how an application can be built to connect with each other. Web service is therefore creating a new integration paradigm -- attempting to tie together systems and services in a loosely-coupled manner. Thus, the benefit of designing interfaces to reduce the interdependencies and increase flexibility across modules or components can be strongly argued for adopting Web services. The above experimentation is but a small example of how Web services can be a valuable addition to many integration projects. To exploit the full business potential created by such loosely coupled technology as Web services, managers will need to creatively design new, more loosely coupled management approaches and take advantage of the flexibility provided by the technology. experimentation concludes the loose coupling premise. This

Das könnte Ihnen auch gefallen