Here's a very simple example on how to invoke and consume a WSDL webservice using serverside Actionscript. You will need Flash Media Server 2 for this, it will not work in Flash Communication Server.
The Webservice I am consuming in this example simply returns the currency exchange rate between two countries. For each country we must pass a string identifying the country, in this case I use 'UK' and 'USA'.
What follows is pure ASC (serverside Actionscript). No .fla file or clientside code is needed to run this.
Here is the content of my main.asc file (you can just copy and paste this code and save it as 'main.asc'):
load( "webservices/WebServices.asc");
application.onAppStart = function() {
// set up our webservice URL. This service returns an exchange rate between 2 countries
var wsdlURI = "http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl";
// create the service rateService = new WebService(wsdlURI);
// Handle the WSDL loading event. rateService.onLoad = function(wsdl){
trace( "--- onLoad");
trace(wsdl);
trace( "--- end onLoad");
}
// If the WSDL fails to load the onFault event is fired. rateService.onFault = function(fault){
trace( "--- rateService onFault");
trace( fault.faultstring );
trace( "--- end rateService onFault");
}
// This invokes the remote method 'getRate' an asynchronous callback. // We use 'UK' and 'USA' as the 2 countries to query. callback = rateService.getRate( "UK", "USA");
// Handle a successful result. This is the important bit :-) callback.onResult = function(result) {
trace( "--- Result");
trace( "Result. The Exchange Rate is: " + result);
trace( "1 Pound Sterling will today buy you " + result + " US Dollar.");
trace( "--- end Result");
}
// Handle an error result. callback.onFault = function(fault){
trace( "--- callback onFault");
trace( fault.faultstring );
trace( "--- end callback onFault");
}
}
In order to run this code you need to create a new application inside your FMS2's applications directory. I called my folder 'wsdl'. Next you should open the Flash Media Server admin console and load the 'wsdl' application. Then hit reload application. You should see the following traces:
WebServices Loaded successfully.
12/16 10:12:34 [INFO] : Queing call getRate
12/16 10:12:35 [INFO] : Made SOAPCall for operation getRate
--- onLoad
String too long to display
--- end onLoad
12/16 10:12:35 [INFO] : Invoking previously queued call getRate
12/16 10:12:35 [INFO] SOAP: Asynchronously invoking SOAPCall: getRate(UK,USA)
param:country1 UK
param:country2 USA
param:Result undefined
--- Result
Result. The Exchange Rate is: 1.7724
1 Pound Sterling will today buy you 1.7724 US Dollar.
--- end Result
Note that the traces such as
param:country2 USA
param:Result undefined
The documentation on webservices with FMS2 leaves a bit to be desired. My example was based on the code samples of in the live docs.