2006-12-29
Consuming PHP SOAP Webservices with Flash - Part I
Yesterday I published version 0.50 of Services_Webservice. A PHP Pear package to easily create Webservices and WSDL. After several tests with PHP and C# I thought it is time to find a better way in Flash to create backend-driven applications. My attention came to mx.services.Webservice, which seemed to be an alternative to loadXML(), sendAndLoad() or loadVars().
First thing to do is creating a simple Webservice. The code looks like this and is available at the following URL:
http://dev.dschini.org/tests/Services_Webservice/test_01.php
-
include_once('Services/Webservice.php');
-
class myService extends Services_Webservice
-
{
-
/**
-
* Says "Hello!"
-
*
-
* @param int
-
* @return string
-
*/
-
public function hello($i )
-
{
-
return 'myString';
-
}
-
}
-
-
$myService = new myService(
-
'myService',
-
'example webservice description',
-
'encoding' => SOAP_ENCODED,
-
'soap_version' => SOAP_1_2)
-
);
-
$myService->handle();
Now if you open that page in your browser the following info page is displayed and shows you details about the webservice you just created. It displays all the available functions, in our case just one: test(), which expects an integer as parameter and returns a string. The description of this method could explain more information about the function use of course. There also is a link to the DISCO file and the WSDL that are created automatically from the Webservice package.

Consuming such a webservice from PHP would most probably look like this:
-
<?php
-
$client = new SoapClient("http://.../test_01.php?wsdl");
-
?>
Since this post is about consuming webservices with Flash I will do the exact same thing with Actionscripting >= 2.0. Please note that the clientPendingHello Object is a Pending Object which envokes the onResult or onFault event handler right after the serialization of the function call.
-
import mx.services.WebService;
-
-
var client = new WebService("http://.../test_05.php?wsdl");
-
clientPendingHello = ws.hello(5);
-
clientPendingHello.onResult = function(result) {
-
var a = result; // set breakpoint here
-
}
To see if this works I set a Breakpoint in the onResult Event Handler and start the application in Debug Mode to analyse the result variable.

The variable with the name "a" has the value ="myString". As you can see this works just fine and is a much better and easier way of getting data from a server. I hope I can convince some of you Flash Developers out there to give Services_Webservice a try and see how easy it is to create backend-driven applications in PHP. In the next part of this series I will try show some example how to share complexe objects between PHP and Actionscript.
7 Comments
-
There is a little bug in the Actionscript version of the Webservice client. Here is the correct code:
import mx.services.WebService;
var client = new WebService(”http://…/test_01.php?wsdl”);
clientPendingHello = client.hello(5);
clientPendingHello.onResult = function(result) {
var a = result; // set breakpoint here
} -
Manfred Weber’s Blog: Consuming PHP SOAP Webservices with Flash - Part I
-
I’ve used SOAP with Flash, and with great success for several years. Setting up the WSDL on the PHP side is frustrating at first. Recently we switched to amfphp instead. It allows you to pass complex data structures, such as arrays, between flash and php much easier. It was very easy to setup and integrated with my existing code in a snap. I don’t have hard data to prove it, but it may actually be a little faster with larger volumes of data. http://www.amfphp.org
-
Hi! Very nice site! Thanks you very much! qt2MghJvGCYxL
-
[...] 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. [...]
-
[...] Consuming PHP SOAP Webservices with Flash - Part I [...]
-
I received this error:
Deserializing parameter ‘x’: could not find deserializer for type {http://classe.com}WsX

