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:

  • Bookstore/Author which defines a single author
  • Bookstore/Book which defines a book plus a Bookstore_Author[] Array
  • Bookstore and finally the Bookstore service

    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.

    Actionscript:
    1. import mx.services.*;
    2.  
    3. var bookStoreLog:Log = new Log();
    4. bookStoreLog.onLog = function(txt:String) {
    5.   trace(txt);
    6. }
    7.  
    8. var bookStore:WebService = new WebService(
    9.   "http://services.dschini.org/Bookstore.php?WSDL",
    10.   bookStoreLog);
    11. bookStoreAllBooks = bookStore.getAllBooks();
    12. bookStoreAllBooks.onResult = function(result:Object) {
    13.   trace(result);
    14. }
    15. bookStoreAllBooks.onFault = function(fault:Object) {
    16.   trace(fault.faultCode+","+fault.faultstring);
    17. };

    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!

  • Download the Bookstore
  • PEAR Service_Webservice
  • Consuming PHP SOAP Webservices with Flash - Part I
  • More example about Services_Webservice
  • 9 Comments

    1. developercast.com » Blog Archive » Manfred Weber’s Blog: Sharing complex objects between PHP and Flash - Part II 2007-02-26, 4:36 pm

      [...] 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. [...]

    2. Tijuan 2007-02-28, 10:01 am

      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).

    3. Evert 2007-03-1, 6:52 pm

      Pretty interesting.. I’ll keep this bookmarked..

      I need to get into SOAP for my job pretty soon, and so far everything i touched turned out pretty horrible in terms of interoperability..

      Maybe for an idea of part III, use PHP5’s built-in soap stuff. It should be a lot faster than the PEAR stuff..

    4. Manfred Weber 2007-03-2, 1:32 pm

      Hi Evert, this Webservice package is built on PHP5´s built-in soap. It is not more then an overlaying wrapper which generates the wsdl automatically and instanciates a PHP5 Soap Server.

    5. Evert 2007-03-3, 10:24 pm

      oops didn’t know that =) thnx

    6. flash developer 2007-10-3, 12:00 am

      Um, where’s the info on complex objects?!?!

    7. Manfred Weber 2007-10-3, 12:42 am

      this is the example of complex types. getAllBooks() returns an array of objects. ok maybe I should do 1 more example of an array of objects where some properties hold objects again.

    8. PHP and Flex - Part III > Manfred Weber`s Weblog 2007-10-9, 9:27 pm

      [...] Sharing complex objects between PHP and Flash - Part II [...]

    9. Dave Stewart 2008-03-1, 7:21 pm

      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

    Add a Comment