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.
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.)
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.
Types of Web Services
As you browse the Internet looking for Web services, and as you look at the Amazon material, you may see a lot of references to different types of Web services, using terms such as SOAP and XML-RPC. Heres what you need to know about this: Although all the Web services we e interested in work by sending and receiving XML over an HTTP protocol, there are several different ways to do this. HTTP requests are divided into broad categories, called GET and POST. A GET request passes information in the URL: If youve ever seen a long, ugly-looking URL in your browser, like http://my.ecommerce-site.com/cartApp.astj?userID=ED45jUiJJ&sessKey=6a45Rtfe4, you e looking at information being sent via a GET request. On the other hand, if youve ever filled out an HTML form and clicked the Submit button, odds are the data was being sent behind the scenes, in whats called a POST request.
Web services may work with either GET or POST requests. FileMakers XML Import feature, however, can make only a GET request. But a number of the most common Web services techniques work exclusively via POST.
SOAP is an example of such a Web services protocol. SOAP transactions send a complex XML request to the server via a POST operation. Many Web services are available only via SOAP. A good example is the Google Web service, which lets you interface directly to the Google search engine. Because Google is SOAP-based, there is no direct way for FileMaker to interact with the Google Web service at the moment.
In looking for FileMaker-ready Web services, you e looking for Web services that support simple XML over HTTP via GET requests.
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: Genet&mode=books&type=lite&page=1&f=xml
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.
Lets say we have a book database with the field structure shown in Figure 22.8.
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.
[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.
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.
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 publisher:Anchor&mode=books &type=lite&page=1&f=xml
(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.
Part I: Getting Started with FileMaker 8
Using FileMaker Pro
Defining and Working with Fields
Working with Layouts
Part II: Developing Solutions with FileMaker
Relational Database Design
Working with Multiple Tables
Working with Relationships
Getting Started with Calculations
Getting Started with Scripting
Getting Started with Reporting
Part III: Developer Techniques
Developing for Multiuser Deployment
Advanced Interface Techniques
Advanced Calculation Techniques
Advanced Scripting Techniques
Advanced Portal Techniques
Debugging and Troubleshooting
Converting Systems from Previous Versions of FileMaker Pro
Part IV: Data Integration and Publishing
Importing Data into FileMaker Pro
Exporting Data from FileMaker
Instant Web Publishing
FileMaker and Web Services
Custom Web Publishing
Part V: Deploying a FileMaker Solution
Deploying and Extending FileMaker
FileMaker Server and Server Advanced
Documenting Your FileMaker Solutions