Xem mẫu

SOAP SOAP SOAP (which originally stood for Simple Object Access Protocol2) is an XML-based protocol with roots in XML-RPC. As with plain XML-RPC, SOAP relies on libraries of code to create and decode the XML commands that magically allow your code to call code on the remote machine. In theory, you should never have to deal with the complexity of SOAP—your chosen library should take care of it all for you. However, in reality, getting to a point where you can actually work with SOAP—getting a library in place, figuring out its API, and deciphering the cryptic error messages that appear when some-thing unexpected happens—can prove to be fairly complex and frustrating in its own right. WSDL WSDL (Web Services Description Language) reduces the complexity of SOAP by providing a description of the web service that your code can use in setting up its SOAP client. It includes information such as the URI that should be used to make queries, and the functions and datatypes the service supports. A WSDL file is an XML-based document that acts something like a remote config file for your SOAP client. Ideally, you should be able just to pass your SOAP client the URI of the WSDL file, and have it figure out how to use the service. Using WSDL with PHP’s SOAP extension looks something like this: $soapClient = new SoapClient("http://badscifi.com/api/MovieSearch.wsdl"); $result = $soapClient->doMovieSearch($key, $movieTitle); It should be as simple as that—once you get your library set up and figured out, that is. Network-centric vs Application-centric REST is a network-centric protocol, meaning that it was designed with the idea of making network communication easier, without a heavy focus on the applica-tions that have to work with it. 2 The SOAP acronym was officially changed by the W3C in version 1.2 of the specification, when it was realized it wasn’t simple at all. SOAP now stands for … SOAP. 171 Licensed to siowchen@darke.biz Chapter 6: Web Services and Slide-and-hide The pieces of your app are expected to comprise a “resource” with a URI, and to respond to HTTP request “verbs” like GET, POST, and PUT. This makes the process simple from the network communication side of things, but forces your server-side code to figure out what all these simple “verbs” mean to your specific application. Ultimately, you have to map your app to the network and its com-munication. XML-RPC and SOAP take the opposite approach. They are application-centric, mapping the actual logic of the application into the communication process. The HTTP request type makes no difference (although POST is most common), and all the information about how to interact with your server-side code is written out in the XML data used in the communication process. This makes things much easier for the application logic, but more difficult for communication, since the XML needed to transmit all the data is a lot more verbose and complicated. In this chapter, we’ll learn how to access web services with REST. We’ll look at example code that uses XML-RPC and SOAP in the next chapter. Amazon Web Services Client Our project for this chapter will be an AJAX client that accesses one of Amazon’s web services. Our AJAX client will pull down the results of several different product-listing searches on a rotation, and insert the result into a web page. Fig-ure 6.2 shows an example of the search results the client gets for the search “Ruby programming.” 172 Licensed to siowchen@darke.biz Amazon Web Services Accounts Figure 6.2. Amazon web services client showing a set of search results Amazon Web Services Accounts To use this service, you’ll need to have an Amazon Web Services (AWS) account. This will give you an Access Key ID, which you’ll need in order to make queries using the service. Many of the large technology companies that offer web services require you to have this kind of key or token to access their services. This is only natural—they want good data about who’s using their service, and how they’re using it. These keys are usually free to set up and use, although sometimes they may limit the number of queries you can perform per day for free. 173 Licensed to siowchen@darke.biz Chapter 6: Web Services and Slide-and-hide Amazon displays a very obvious link on the AWS home page3 (shown in Fig-ure 6.3) to the information that explains how to create a free AWS account. Once you have your account set up, and your Access Key ID has been emailed to you, you’ll be all ready to get started. Figure 6.3. The link for creating a free AWS account Amazon E-Commerce Service We will be using Amazon’s E-Commerce Service (ECS), one of Amazon’s web services, which provides access to product data and ecommerce functions. This gives you access to the full range of search functionality that’s available from Amazon’s main site. Excellent documentation is available on the Amazon web services site, although it does take a bit of surfing around to find the bits of in-formation you need. The first thing for us to do is to pick the method we want to use to access the ECS web service. Amazon offers access via SOAP and REST. Since our needs are modest, we’ll go with the REST method, so we can get right to work performing a search and pulling down some results. With REST access to Amazon ECS, we’ll be sending AJAX-style GET requests to perform searches. The search details will be specified on the query string. 3 http://www.amazon.com/aws/ 174 Licensed to siowchen@darke.biz The Client Class To make sure the results we receive are easy to parse and work with, Amazon returns its search results as XML. This will be our first foray into the process of consuming XML, and you’ll find that, initially, it can be tricky to get it to work. On the other hand, the nice thing about XML is that it’s very predictable, so once you get the results you want, it’s fairly easy to make adjustments without breaking things. The Client Class For lack of a better term, we’ll call our client class Client. It’s not terribly inspired, but it is accurate: it’s a web service client. To keep our rotating searches ticking over we’ll be using setTimeout and, predictably, we’ll make the class a singleton so that we don’t have to worry about loss of scope. Here’s the initial code: File: webservice1.js (excerpt) var SEARCH_TERMS = [`ajax`, `postgresql`, `ruby programming`, `php`, `javascript`]; var ACCESS_KEY = `Access Key ID`; var Client = new function() { this.SEARCH_TERMS = null; this.ACCESS_KEY = null; this.ajax = null; this.incr = 0; this.containerDiv = null; this.currDiv = null; this.newDiv = null; this.slideInterval = null; this.slideIncr = 380; }; window.onload = Client.init; At the start of this file, we declare a couple of constants for the application. These are included so that a webmaster could set up and configure this code without having to know anything about how it works (in theory, at least). SEARCH_TERMS is an array of search phrases through which the app will cycle. ACCESS_KEY is your AWS Access Key ID. 175 Licensed to siowchen@darke.biz ... - tailieumienphi.vn
nguon tai.lieu . vn