Consuming Web Services with Flash and AMFPHP
Beginning with the release of AMFPHP version 0.5.2, it is possible to consume remote web services using Flash and AMFPHP. To use a web service you will need to download and install one of the two following packages:
- NuSOAP - http://dietrich.ganx4.com/nusoap/index.php
- PEAR::SOAP - http://pear.php.net/package-info.php?pacid=87&version=0.7.2
In this example, both NuSOAP and PEAR::SOAP are usable; the main difference is that users without administrative access to the server will prefer the NuSOAP package while users with higher server privileges will prefer the PEAR::SOAP package.
Installing PEAR::SOAP
If you are the administrator or have access to install pear applications from the command line you can use PEAR::SOAP. To install PEAR::SOAP open up a terminal and su to a user with sufficient privelages to install pear modules. PEAR::SOAP requires a few additional packages so install them first.
This is assuming that PEAR is installed and the pear library is available in your include path.
Run: pear install Mail_Mime pear install HTTP_Request pear install Net_URL pear install Net_DIME
It's a good idea to run the pear update to make sure you have the latest modules.
pear updgrade-all
Then install the soap package:
pear install SOAP
If all of the above commands complete successfully and the PEAR library is available in your include path, then PEAR::SOAP is installed.
Installing nuSOAP
Download the nusoap.php file from the sourceforge.net repository: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/nusoap/lib/. Place the nusoap.php library in flashservices/lib/ folder. You will have to create a lib folder in flashservices if one doesn't exist. That's it, nuSOAP was completely installed and ready to use.
Check / Create your gateway
If you already are using AMFPHP, then there is nothing else you have to do unless you installed BOTH PEAR::SOAP and nuSOAP and would like to use nuSOAP for this example. AMFPHP uses PEAR::SOAP by default and will look for nuSOAP only if PEAR::SOAP isn't available or you set the gateway to not use PEAR::SOAP. If this scenario applies to you open up your gateway and add:
$gateway->usePearSOAP(false);
Replace $gateway with your Gateway class instance name and add this line before the $gateway->service() call.
If you are creating a gateway, follow the instruction (here) and add the above line before the $gateway->service() call.
Create a client
Now that your AMFPHP installation is ready to consume web services open up Flash MX and create a new document. For this example we will be working entirely in the Actions Panel and not creating any interfaces.
Open up the Actions Panel for the first frame. The format of this tutorial will be source code then documentation so you novice and advanced users can easily skip the details if you would like.
Using NetServices
#include "NetServices.as"
NetServices.setDefaultGatewayURL("http://yourdomain/flashservices/gateway.php");
net_conn = NetServices.createGatewayConnection();
This first thing to do is include the NetServices ActionScript Library. This library contains many useful functions and wrappers for the NetConnection object so you the developer can call remote methods as if they were internally defined with ActionScript. The NetServices class is useful but it is not necessary. However, this tutorial will not work without it.
Next you have to map your gateway url with the NetServices object with the .setDefaultGatewayURL method. It takes a single parameter, the URI of the gateway.
Finally here you need to create the NetConnection object with NetServices.createGatewayConnection();
Making an error handler
_global.System.onStatus = function (error)
{
for (var i in error) trace( i + error[i]);
}
Since errors do happen during this process we create an error handler so any errors returned by the server will be displayed through the trace window.
Calling the Temperature Service
Temperature_service = net_conn.getService("http://www.xmethods.net/sd/2001/TemperatureService.wsdl");
temperature_responder = {
onResult:function (data) {
this.currentTemperature = data;
trace("The temperature service responded");
}
}
Params = {
Zipcode:"60606"
}
temperature_service.getTemp(temperature_responder, params);
The first thing we want to do is create the proxy stub object to the remote service. This allows us to call the operations defined in the wsdl as local methods in flash. You must pass the full uri of the wsdl file to the getService method.
The next thing to do is create a responder object to handle the results from the remote method. A responder object has to have at least on onResult method defined. This is the default method that gets executed when data is returned by the server. In the onResult method we just save the returned data as currentTemperature and output to the trace window a message that lets us know that the temperature service replied.
Next we have to create a parameter object. A parameter object is an value object that contains the name/value pairs of data that the web service expects. The use of a value object is preferred so that there is not confusion what value is mapped to which parameter.
Finally we execute the remote method "getTemp" while passing it the responder object as the first parameter and our params object as the final parameter.
temperature_service.getTemp(temperature_responder, "60606");
PEAR::SOAP requires that you use the value object, and the best practice is using a value object regardless of which SOAP package you installed.
Calling the BabelFish Service
Babel_service = net_conn.getService("http://www.xmethods.net/sd/2001/BabelFishService.wsdl");
Babel _responder = {
onResult:function (data) {
this.translation = data;
trace("The babelfish service responded");
}
}
Params = {
Translationmode:"en_fr",
Sourcedata:"This is text to be converted into another language"
}
Babel _service.BabelFish (Babel _responder, params);
The above code follows the exact same steps except the params object now defines two name/value pairs, translationmode and sourcedata. These are the two arguments that the BabelFish service expects.
---
For additional web services to try out: Visit the Flash-db web services Directory.

5 most recent
Flash button as Flex icon
Tree menu
Flash Spell Checker
Flash Remoting Library
MX 2004 Chart/Poll
Articles