Table of contents
- Web Service small how-to
- Flex and PHP web-service
Yesterday I had to do a small flex application that resided on a server with php. Since I required a connection to mysql I wanted to use the same class I did before to connect to web-services. But PHP does not support web-services (and WSDL) by default.
So after I did some search on the net I found this nice php class: NuSOAP. Here it is the small php webservice I did to test it all (file test.php):
// Pull in the NuSOAP code require_once('nusoap.php'); // Define namespace to use $ns="http://localhost/wsdlphp"; // Create the server instance $server = new soap_server; // Configure WDSL response $server->configureWSDL('hello',$ns); // Set default namespace for output $server->wsdl->schemaTargetNamespace=$ns; // Register the method to expose $server->register('hello', array('name' => 'xsd:string'), array('return' => 'xsd:string'), $ns ); // Define the method as a PHP function function hello($name) { return 'Hello, ' . $name; } // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA);
As you see here there is one service called test.php and one webmethod called hello. This method has one parameter as input (name):
$server->register('hello', array('name' => 'xsd:string'), array('return' => 'xsd:string'), $ns);
Here is the flex code to connect and use the webmethod:
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> applicationComplete="init()"> <mx:script> <![CDATA[ import mx.rpc.soap.Operation; import mx.rpc.events.InvokeEvent; import mx.rpc.soap.LoadEvent; import mx.rpc.events.FaultEvent; import mx.rpc.events.ResultEvent; import mx.rpc.soap.WebService; import ro.qbic.testmessage; private var _service : WebService; private var _currentMessage: testmessage; public function init():void { _currentMessage = new testmessage("a"); _service = new WebService(); _service.wsdl = "http://localhost/WSDLPHP/test.php?wsdl"; _service.addEventListener(ResultEvent.RESULT, handleResult, false, 0, true); _service.addEventListener(FaultEvent.FAULT, handleFault, false, 0, true); _service.addEventListener(LoadEvent.LOAD, handleLoad, false, 0, true); _service.addEventListener(InvokeEvent.INVOKE, handleInvoke, false, 0, true); _service.loadWSDL(); } public function handleResult(event:ResultEvent):void { // Get data from RESULT node var tmpResp:XML = event.result[0] } private function handleFault(event:FaultEvent):void { var errorMessage:String = event.fault.faultString + "\n" + event.fault.faultDetail; } private function handleLoad(event:LoadEvent):void { _service.getOperation(_currentMessage.webMethod).arguments = _currentMessage; (_service.getOperation(_currentMessage.webMethod) as Operation).resultFormat = "e4x"; _service.getOperation(_currentMessage.webMethod).send(); } private function handleInvoke(event:InvokeEvent):void { } ]]> </mx:script> </mx:application>
Popularity: 74%
Tags: flex, PHP, webservice
This post was written by Virgil Cristea
Views: 3531



















I’m using AMF.PHP - which in fact, introduced me to Flex - which seems much simpler/faster, in a sense of developing. Have you tried it?
Regarding security issues (from previous post in series), besides setCredential, you can use a internal appID which is sent with every message to the service (build your Flex class this way) - so you make sure that your application is the only one that uses your service.
Yes I did try some AMF some time ago. The reason why I chose xml over amf was the market penetration of XML. In cases I return the data directly from MSSQL as an XML and I not want to add another layer to convert it to AMF on the webservices (they are also used by non-Flex Apps).
About security yes you can use setCredential, tokens or even WS-Security methods (I know that Adobe did not implement them in flex but there is a way to make it work.. in the next article).