9.4 Using AMFPHP with Web Services

If you'd like to use AMFPHP to connect to pre-existing SOAP-based web services, you don't have to write a single line of PHP. As long as your installation of AMFPHP is set up properly, connecting to web services with it is a breeze and is nearly identical to the process you would with ColdFusion (ColdFusion is the only other Remoting gateway that automatically supports connecting to external web services). See Chapter 10 for more information about web services with ColdFusion and .NET.

Not all web services work with AMFPHP. This is a limitation of the SOAP libraries that AMFPHP uses rather than a limitation of the AMFPHP library itself. If you want to test to make sure a particular web service is accessible before you start doing heavy development against it, download the CMX Remoting Testing Tool from http://www.communitymx.com/abstract.cfm?cid=E79F1303C1E096ED. The tool is a free extension that adds a new CMX Remoting Testing Tool panel to the Flash MX authoring environment and allows you to quickly execute remote methods and see the results.

To use a web service with AMFPHP, you first need to know the URL of that web service's WSDL file. A WSDL file is an XML file that describes a web service, including its URL, the methods it supports, and the arguments and return types of those methods. You can, in fact, open a WSDL file in a text editor and read this information from it. Of course, you also need to know the URL of the gateway.php file up on your server.

The only slightly odd process in using a web service with AMFPHP is that you must pass all of the arguments to the web service via properties of an object. For example, XMethods (http://www.xmethods.com) provides a free web service that returns the current temperature for any U.S. Zip Code. The WSDL file for this web service dictates that there's a single method named getTemp( ), which expects a single argument specifying the Zip Code. Example 9-1 shows the client-side ActionScript code that connects to this web service and prints the result to the Output window.

Example 9-1. Accessing the getTemp( ) web service via Flash Remoting
#include "NetServices.as"

onResult = function (temp) {
  trace("Got temp: " + temp);

gatewayURL = "http://localhost/frdg/gateway.php";
serviceURL = "http://www.xmethods.net/sd/2001/TemperatureService.wsdl";
gateway = NetServices.createGatewayConnection(gatewayURL);
service = gateway.getService(serviceURL);
service.getTemp(this, {zipcode:20781});

Example 9-1 creates an anonymous object to transmit the arguments to the getTemp( ) remote service. You could also pass the arguments by attaching properties to a generic instance of the Object class:

data = new Object( );
data.zipcode = 20781l
service.getTemp(this, data);

When the web service is written in ColdFusion, you can often get away without creating such an object and just calling the service like this:

service.getTemp(this, 207811);

    Part III: Advanced Flash Remoting