Working with Web Services
The previous section, on importing XML via a stylesheet, tells you more or less all you need to know to work effectively with Web services in FileMaker. The only other thing you need is a real Web service to work with.
It can actually be a bit difficult to find interesting Web services to play with. Many of the really meaty Web services, because they e providing useful information, charge an access or subscription fee of some kind. (These might include Web services that provide current weather information from satellites, or financial information, for example.) Many of the free Web services, by contrast, are either of limited scope, or else represent hobby work, student programming projects, and the like.
Happily, there are a few exceptions. We e going to take a look at Amazon.coms Web service offerings. Amazon, of course, has a user interface, presented via HTML, that you can use to conduct Amazon searches by pointing and clicking with your mouse in a Web browser. But Amazon has also been a pioneer in offering XML-based Web services that let you do the same thing, allowing you to integrate Amazon data into other applications.
Suppose that you have a FileMaker database containing information about books. For each book youd like to be able to check whether its available from Amazon, and if so, at what price. With FileMakers XML Import capability, you can do this fairly easily.
Accessing the Amazon Web Services
Working with Amazons Web services is straightforward, but you need to do a couple of things first. You should visit http://www.amazon.com/webservices; there, youll be able to download the Web services developers kit, which provides useful documentation, and youll also be able to apply for a developers token, which is a special personal key youll need to send along with your Web service requests for validation. (Theres no charge for either the developers kit or the token.)
Note
If you don want to take the trouble to apply for your own developers token, you can use one that we requested for use in this book. That token is D1AT17BPIA1PX7.
The developers kit comes with documentation that shows how to formulate various types of HTTP requests for data. Heres a sample URL:
[View full width]
http://xml.amazon.com/onca/xml3?t=xxx&dev-t=D1AT17BPIA1PX7 &PowerSearch=title:
This searches Amazon for books with the word "Genet" in the title. Try entering the above URL in Firefox or Internet Explorer 5 or greater (which render the resulting XML nicely), and youll see what the returned results look like. This returns data in Amazons lite format, which has less information than the corresponding heavy format.
Amazons XML format, whether lite or heavy, is clearly not FileMakers FMPXMLRESULT. So if you want to bring this book data back into FileMaker via an XML import, you need a stylesheet to transform it appropriately on the way in.
Writing a Stylesheet to Import Amazon Data
Lets say we have a book database with the field structure shown in Figure 22.8.
Figure 22.8. Field structure for a database of book information.
We can bring Amazon data into this FileMaker structure by performing an import from an XML data source and applying a stylesheet to the inbound data. The stylesheet looks and works a lot like the one in Listing 22.8. We show it here, for completeness, as Listing 22.9.
Listing 22.9. Stylesheet for Transforming Amazon XML into FMPXMLRESULT XML
[View full width]
|
This is extremely similar to the earlier stylesheet, even down to the treatment of book authors, which occur in nested groups: Here, we loop over authors in the same way we looped over parent records, flattening them into a single text field.
Building a More Flexible Interface to a Web Service
The previous section concentrated on the stylesheet that you would use to import data from Amazon. But so far, weve just assumed that FileMaker is issuing some hard-coded URL to perform an Amazon search. In fact, we probably want our users to be able to compose their own queries and submit them to Amazon.
Theres no great mystery to this. The Amazon developers kit documents the different types of search strings that the Amazon Web service can accept. (If we e just searching for books, a lot of the more interesting options can be found as part of the overall "power search" option.)
So far, weve imported XML only from data sources we specified via a hard-coded URL. Its also possible, though, when importing XML into FileMaker via a script, to draw the XML from a data source specified by a calculation. Figure 22.9 shows the relevant dialog choice.
Figure 22.9. When importing XML into FileMaker via a script, you can use a calculation to create the source URL on the fly.
This makes it possible to compose the Amazon URL on the fly based on user input. For example, if we wanted to search for books by Naguib Mahfouz, published by (say) Anchor, the Amazon URL would look like this:
[View full width]
http://xml.amazon.com/onca/xml3?t=xxx&dev-t=D1AT17BPIA1PX7&PowerSearch= author:Mahfouz and
(In the real URL, you would use your Seller ID, if you had one, instead of the nonsense string xxx.) To compose this URL dynamically, youd need to offer the user a couple of global fields in which to type. Assume that the user called gAuthorSearch and gPublisherSearch. You could then define a calculation field that would look something like this:
http://xml.amazon.com/onca/xml3?t=xxx&dev-t=D1AT17BPIA1PX7 &PowerSearch=author:" & gAuthorSearch & "and publisher:" & gPublisherSearch & "&mode=books&type=lite&page=1&f=xml"
And, as shown in Figure 22.9, you can instruct FileMaker to derive the URL from a calculation, which could point directly to this dynamic field. This snippet is really useful only as an example of how you might go about this conceptually. In reality, youd need to do some work to build a really nice interface to Amazon. Youd want to add fields for all the types of Amazon searches (there are about seven). Youd also want to provide for the fact that the user might choose to search on some but not all criteria, making it a good idea to omit the unused search types from the URL. Youd have to account for the fact that its possible for searches to have multiple words, in which case they need to be enclosed in quotes. And youd want to account for the different search types Amazon allows, such as searching by exact match or initial match.
Категории