The Server Side
Flash can't load data from an outside domain. This being the case we need to use some type of script that acts as a proxy and also handles all of the details of sending and receiving soap based messages. We have quite a few options available to use for this step. Their are literally hundreds of different Soap toolkits that can handle this part for us. The most relevant ones would be the MS .net soap toolkit from Microsoft, Flash Remoting which requires Coldfusion or a special component (Salsa) installed on your server, and of course either of the PHP soap toolkits. A soap toolkit is basically a couple of classes and functions that handles all of the details of sending and receiving soap based messages. So what did we choose for this tutorial, well of course one of the PHP toolkits.
Nusoap/SOAPx4: In this example we are going to use Nusoap (aka SOAPx4). We are using Nusoap because it only requires 1 php file to be placed anywhere on any server. This also allows us to use web services with any computer running PHP. By any I mean any - You can build your application on any windows, Mac, Linux desktop and deploy it on any Windows, Mac, Unix, Linux Server. So it's tough to beat a setup that only requires 1 file on any server. Their are no special installation or server setup worries, their is nothing to buy, and you can build your own Soap based web service just as easily as sending a soap message. The main disadvantage of Nusoap is that at the time of this writing it is not as mature as some of the other toolkits (.Net) and has some growing to do. But it works for any web service and great for the example so we'll let it go at that. So here goes:
First: You will need to place the Flash Client we just created, a file called 'nusoap.php' (in the download), and the following code in the same directory on your server. In the future you can change around the paths - as only 1 nusoap.php file is needed for all clients. Also you might want to experiment with setting it up so that the path to WSDL file and operation/method to be used - is sent from the flash movie instead of being specified in the script, this creates a more generic soap client - but generic solutions can also cause problems and are limiting).
< ? $Mode = $HTTP_POST_VARS[Mode]; $sourceData = $HTTP_POST_VARS[sourceData]; // Include the NuSoap Class's. require_once('nusoap.php'); // Add necessary Parameters. $parameters = array( "translationmode" =>$Mode, "sourcedata" =>$sourceData ); // Create a new object $soapclient = new soapclient('http://www.xmethods.net/sd/2001/BabelFishService.wsdl','wsdl'); // Call the service. $result = $soapclient->call('BabelFish',$parameters); // Print the result. print "&soapOut=$result&"; ? >
The first 2 lines are needed to indicate the variables coming into the script from flash via 'POST'. The next line includes the Nusoap Soap toolkit classes needed for the details. Then we specify the input parameters, in this case translation mode and sourcedata are required. Then we need to create a new soap Client. In this example we specify the path to the WSDL file (Web Service description language), the WSDL file describes the service that we are going to use - and indicates all of the important parts in an XML format. The WSDL file is parsed by the toolkit and used to call the service (among other things). Usually a Service has a couple of available operations or methods in which you can use - these are also specified in the WSDL file. In this case we are using the 'BabelFish' operation/method. When we call this script - that operation (basically a function on some other server) is invoked and a result is returned. The variable $result holds the result and is passed back to flash on the last line. This is a simple case - in most case's the $result will hold an array or other more complex structure's (like with Google's API) - that needs to be parsed before it's sent back. Usually it's only 1 or 2 additional lines though.
Well this tutorial is not on WSDL so I'll spare the details - but usually the most important part of the WSDL file is the location of the EndPoint - the Endpoint is where the service actually/physically resides. In the above script you can skip the middle WSDL file and indicate the exact endpoint if wanted.
To visually see the WSDL file - you can use the following Tool, which is a Flash application that Parse's a WSDL file and describes everything in that file in an easy to read/use Flash interface - the link to that for the BabelFish service is as follows:
Well that's about it. We'll finish up with some links to additional resources and a few closing words.
|» Level Advanced|
Rating: 7 Votes: 10
|Jeffrey Hill is a freelance web developer from Boulder, Colorado. He specializes in creating and developing dynamic database driven Flash content, applications, and content management systems. Specialty's include SQL, PHP, Perl, XML, web services, and Flash clients for web services.|
|Download the files used in this tutorial.|
|Download (251 kb)|
|More help? Search our boards for quick answers!|