2007-10-9
PHP and Flex - Part III
Today I read an article on PHPBuilder titleing PHP and Adobe Flex. The author shows a way how a Flex client reads xml data from a php backend.
So why not do the same with Flex and the PEAR package Services_Webservice which I think is a smarter way, if you want to use XML at all. Smarter because you use Web Standards and developers do not need to learn and parse the returned XML structure .
OK I will start with the most simple example. A very simple Webservice. Have a look at it here.
This Webservice only has a single method "hello" which expects 1 parameter (an int) and returns a string.
Sources for this Webservice are available here.
Next thing is to create a first Flex Client. Here is the source:
-
<?xml version="1.0"?>
-
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
-
<mx:Script>
-
<![CDATA[
-
import mx.controls.Alert;
-
]]>
-
</mx:Script>
-
<mx:WebService id="myService"
-
wsdl="http://dev.dschini.org/.../test_01.php?WSDL"
-
fault="Alert.show(event.fault.faultString), 'Error'">
-
<mx:operation name="hello" resultFormat="object">
-
<mx:request><i>1</i></mx:request>
-
</mx:operation>
-
</mx:WebService>
-
<mx:Panel title="myService Example"
-
height="400" width="400"
-
paddingTop="10" paddingBottom="10"
-
paddingLeft="10" paddingRight="10">
-
<mx:Button label="say hello to php"
-
click="myService.hello.send()"/>
-
<mx:Text htmlText="Result: {myService.hello.lastResult}"/>
-
</mx:Panel>
-
</mx:Application>
Compile it using the free mxmlc compiler and you'll have your first client [Example].
OK, but returning a string is too easy. So let's try something more complex. Maybe a simple Registration eh? OK, here is the Webservice. Notice that we deal with objects now! And here is the Flex client.
So basically we just send a RegistrationModel Object to the Webservice and return the exact same Object. Feel free to play with this example. Here is the Flex Source and here is the PHP Webservice Code.
You can do lots more with Services_Webservice if you want. Here're some more examples:
- Basic Examples
- Consuming PHP SOAP Webservices with Flash - Part I
- Sharing complex objects between PHP and Flash - Part II
Have fun!
4 Comments
-
Looks nice, I have read your other articles and your web service class seems very interesting. Maybe I am not that familiar with webservices, how would you send back errors and process them through flash. Like sending back messages like “Not authorized” or “invalid user/password”. How can you trigger an error programatically!
-
Hi Mike,
good question … a soap server can throw SOAP FAULTS (similair to an Exception) which the client has to catch.
Authorization is a wide field. You might protect your server with HTTP Basic Auth but a more secure method is to always pass something like a session key which the client received after calling a custom login function. Well I am not an expert in that field but I remember that Rob Richards was working on implementing WSSE [link].
