2007-02-23
Sharing complex objects between PHP and Flash - Part II
This is part 2 of my SOAP series. In the 1. part I started with a basic example of creating a simple PHP Soap Server with the PEAR package Services_Webservice. In this part I will share complex objects between PHP and Flash.
So I basically created 3 PHP classes:
Now calling the webservice in the browser we'll see that everything works like expected. The Webservice should serve 1 function "getAllBooks()" which returns a list of Book classes which hold an array of Bookservice_Author[] classes within its properties.

Quite simple, no? This took me less then 20 minutes. We now have a full Webservice running! Next step is to connect Flash against it and see if it can handle these complex types. Below is a little Actionscript client which makes use of the mx.services package. Not only we use the WebService class, we also add the Log class which helps you analyse WebServices.
-
import mx.services.*;
-
-
var bookStoreLog:Log = new Log();
-
bookStoreLog.onLog = function(txt:String) {
-
trace(txt);
-
}
-
-
var bookStore:WebService = new WebService(
-
"http://services.dschini.org/Bookstore.php?WSDL",
-
bookStoreLog);
-
bookStoreAllBooks = bookStore.getAllBooks();
-
bookStoreAllBooks.onResult = function(result:Object) {
-
trace(result);
-
}
-
bookStoreAllBooks.onFault = function(fault:Object) {
-
trace(fault.faultCode+","+fault.faultstring);
-
};
Again we add a Breakpoint to analyse the result object within the function call to see if the objects are serialized properly. Below is the screenshot of my Flash Debugger to demonstrate that everything works just fine.

And now? Well, the time of using sendAndLoad() ends! And there's no need for writing custom XML Serializers to share data between server and client. Lot's of people pointed to AMFPHP which might be an alternative to SOAP of course. However, you decide!
9 Comments
-
[...] Manfred Weber has posted part two today of his look at sharing more complex objects between PHP and Flash. This time, he focuses on implementing the classes he’d created before. This is part 2 of my SOAP series. In the 1. part I started with a basic example of creating a simple PHP Soap Server with the PEAR package Services_Webservice. In this part I will share complex objects between PHP and Flash. [...]
-
Nice Job !
But like you and other said, the alternative can be amfPHP, and that’s what I generally use for flash-PHP application (flash games, flash dynamic websites, and so on).
But it is still a good example of what can be done and how easier it is to make flash communicate with a server side language like PHP than before with the crappy loadVars for instance (that we still use for small needs indeed).
-
Hi Manfred,
I’ve just come back to Flash from PHP after about a year’s absence, and was really surprised to hear everyone slagging off LoadVars, as I’ve been sending complex variables *natively* to PHP from HTML forms all year.
So I’ve wrapped up all this goodness in a Flash class PHPLoadVars (also works with Ruby, so I’m told) which allows you to send any multidimensional object’s variables direct to PHP, with no server-side scripting at all.
Your method is great for the receiving side as well, but for a basic send, I don’t this can be beaten.
More information here:
http://www.keyframesandcode.com/code/development/flash/actionscript/flash-phploadvars-class/Cheers,
Dave
