This Tutorial goes over how to use Flash Remoting with AMF (Actionscript message Format), PHP, and Web Services.
We will be creating a generic PHP client script that can access any Web Service and return the results back to Flash with Flash Remoting.
First we need to set a couple things up.
Create a new directory on your server, you will then need to place the following 2 files in that directory.
1: Nusoap.php, this is a Soap Toolkit for PHP. Nusoap will handle all Soap based requests for you. You can download the latest version of Nusoap from here:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/nusoap/lib/nusoap.php Click the 'view' link then copy, paste, and rename the file nusoap.php.
2: amfdata.php, this is a PHP class that parse's amf data. This is used so that native array's and objects in Flash and PHP can communicate (be used by each interchangable). You can find the latest version here:
http://www.fontimages.org.uk/flash/amfdata_php.html (Thanks to Musicman for this!)
Place both of these files (nusoap.php and amfdata.php) in the same directory.
Now we need to create a Client script on the server that utilize's both of these files. Lets call this file 'amfSoapClient.php'. This will be able to handle any request to almost any webservice.
amfSoapClient.php
<?php
// Include the NuSoap Class to handle Soap based requests.
include('nusoap.php');
// Include the AMF (ActionScript Message Formatter).
include('amfdata.php');
// Create a new connection to the AMF parser.
$request = new amfdata();
// Create a new Soap Client object. The first paramter of this is the WSDL file location.
$soapclient = new soapclient($request->fnargs[1],'wsdl');
// Call the service.
$data = $soapclient->call($request->fn,$request->fnargs[0]);
$request->sendresult($data);
?>
If your familar with using Nusoap this should look familar - If not try out some of the examples on this site or read some of the other tutorials dealing specifically with nusoap.
Some important parts to mention about this script:
$request = new amfdata(); - creates a new instance of amfdata that will be used to convert between actionscript and PHP.
The following variables:
$request->fn - will contain the function name. This will be a bit more clear when we look at the flash side.
$request->fnargs[ 0] - contains an array of parameters that we sent from Flash. These parameters are used as the input parameters for the Soap request.
$request->fnargs[ 1] - In this case contains the path to the WSDL file.
Now lets take a look at the Flash side. This will make things a bit more clear:
(In this case we are using the Babelfish Web Service which converts between various languages).
ActionScript[script]
// Set up a new Net Connection to be used with Flash remoting.
var nc = new NetConnection();
nc.connect("amfSoapClient.php");
// This is the on Result handler. AKA the function that is called when a result is returned from amfSoapClient.
var BabelFishResult = {};
BabelFishResult.onResult = function(result) {
// This will set the dynamic text box with instance name Output equal to the result of the service.
Output.htmlText = "<font color=\"#ffffff\">"+result+"</font>";
};
// This sets up a New Parameters Object - These parameters are sent to the Actual Web Service.
parameters = new Object();
parameters.translationmode = "en_fr";
parameters.sourcedata = "This is the text that will be converted to French";
// The WSDL file location of the Web Service to Call.
wsdl = "
http://www.xmethods.net/sd/2001/BabelFishService.wsdl";
// Call the Service.
// parameter Order ("methodName", "onResultFunction", "ServiceParameters", "WSDL File")
nc.call('BabelFish', BabelFishResult, parameters, wsdl);
[/script]
And that's about it. We can change this around to access many other Web services. One important thing to remember is that with Nusoap the parameters have to be an Object - which is basically and associative array.
So lets do another example:
(This one will return Stock Quotes and other Company info).
With everything else the same - all we have to change on the Flash side to call a different web service is the following:
[script]
var getStockQuotes = {};
getStockQuotes.onResult = function(result) {
// This will set the dynamic text box with instance name Output equal to the result of the service.
Output.htmlText = "<font color=\"#ffffff\">"+result+"</font>";
};
// This sets up a New Parameters Object - These parameters are sent to the Actual Web Service.
parameters = new Object();
parameters.username = "any";
parameters.password = "any";
parameters.ticker = "macr";
// The WSDL file location of the Web Service to Call.
wsdl = "
http://www.flash-db.com/services/ws/companyInfo.wsdl";
// Call the Service.
// parameter Order ("methodName", "onResultFunction", "ServiceParameters", "URL to WSDL File")
nc.call('doCompanyInfo', getStockQuotes, parameters, wsdl);
[/script]
We'll Hopefull that's enough to help get you started. Please post any questions or problems that you might have below.