Consuming a Web Service with Flash ActionScript 2.0

August 29th, 2007

This is part of my notes while I'm trying to create a Web Service in PHP to be consumed by a SWF application written in ActionScript 2.0.

Consuming a Web Service with ActionScript 2.0

This part is a bit easier. Since the release of Flash MX 2004, the Web Service Classes are available.

First, we create an instance of the WebService class:

Actionscript:
  1. import mx.services.*;
  2. service:WebService = new WebService("http://localhost/my_service_folder/MyService.wsdl");

Right after that we set up the onFault callback function that will be triggered if a error occurs.

Actionscript:
  1. service.onFault = function(flt:Object){
  2.   trace("FAULT");
  3.   trace(flt.faultstring);
  4. };

Then we set up the onLoad callback function that will be triggered when the WSDL is loaded.

Actionscript:
  1. service.onLoad = function(wsdl:Object){
  2.   trace("onLOAD");
  3.   testCall();
  4. };

In this case, we automatically call the testCall() method when the WSDL is loaded.

Actionscript:
  1. function testCall(){ 
  2.   var pc:PendingCall = service.helloWorld();

The testCall() method calls the helloWorld method on the web service. When you call a method on a WebService object it returns a PendingCall object.
This object will trigger a onResult callback function when the asynchronous response from the server is received. If an error occurs an onFault callback function is triggered.
So we set up 2 callbacks for the PendingCall object:

Actionscript:
  1. pc.onResult = function(result){
  2.     trace("onResult");
  3.     trace(result);
  4.   };
  5.   pc.onFault = function(fault:SOAPFault){
  6.     trace("onFault");
  7.     trace(fault.faultstring);
  8.     trace(fault.detail);
  9.   };
  10. }

In the example above if the onResult callback is triggered we just "trace" the result into the output window. If the onFault callback is triggered, we "trace" the faultstring and the detail. These are 2 properties of the SOAPFault object.

A full documentation on the Web Service Classes is available at the LiveDocs.

Below I pasted the entire script without interruptions so you can copy and paste it.

Actionscript:
  1. import mx.services.*;
  2. service:WebService = new WebService("http://localhost/my_service_folder/MyService.wsdl");
  3. service.onFault = function(flt:Object){
  4.   trace("FAULT");
  5.   trace(flt.faultstring);
  6. };
  7. service.onLoad = function(wsdl:Object){
  8.   trace("onLOAD");
  9.   testCall();
  10. };
  11. function testCall(){ 
  12.   var pc:PendingCall = service.helloWorld();
  13.   pc.onResult = function(result){
  14.     trace("onResult");
  15.     trace(result);
  16.   };
  17.   pc.onFault = function(fault:SOAPFault){
  18.     trace("onFault");
  19.     trace(fault.faultstring);
  20.     trace(fault.detail);
  21.   };
  22. }

3 Responses to “Consuming a Web Service with Flash ActionScript 2.0”

  1. Jesse Barnum Says:

    This code doesn't work. When I copy and paste it I get a syntax error on line 2.

  2. Kapil Pendse Says:

    Hi, very very helpful! Simple, to the point description of how to consumer webservice in actionscript.

    Thanks a ton!!

  3. Spark Says:

    Simple and clean. Well done!

Leave a Reply