« Hand Cursor on a Label
» Flex 360 Conference in Europe

ActionScript, Flex 2, Flex resources, How to, PHP, RIA, Web Service

Flex and PHP web-service

17.02.08 | 2 Comments

Table of contents

  1. Web Service small how-to
  2. 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: 85%

Share and Enjoy:
  • Technorati
  • StumbleUpon
  • del.icio.us
  • NewsVine
  • Reddit
  • Digg
  • Furl
  • co.mments
  • blogmarks
  • Slashdot
  • DZone
  • Taggly
  • YahooMyWeb
  • connotea
  • Webride




Tags: , ,

This post was written by Virgil Cristea

Views: 2649

related

popular

2 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« Hand Cursor on a Label
» Flex 360 Conference in Europe