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>
Tags: flex, PHP, webservice
This post was written by Virgil Cristea
Views: 16041











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).
Hi Virgil Cristea
Nice to talk to you. As I told that I’m passing simple add array value to .net web service. I’m receiving this error:
————–
fault: [FaultEvent fault=[RPC Fault faultString="HTTP request error" faultCode="Server.Error.Request" faultDetail="Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://localhost:1474/IPosRepositoryWebservice1/Service.asmx"]. URL: http://localhost:1474/IPosRepositoryWebservice1/Service.asmx“] messageId=”D32F9543-F17A-4296-DE42-C4BBC73144BF” type=”fault” bubbles=false cancelable=true eventPhase=2]
—————–
net code.
——————
Service.vb
——————
<WebMethod()> _
Public Function Addsample(ByVal ParamArray x() As Object) As
Integer
'ByVal ParamArray x() As Object
Dim intRowCount As Object
Dim strSQLselect As String = "Select id from mk where id=" & x
(0)
Try
Addsample = DAL1.Check_Availability(strSQLselect)
If x(0) = Addsample Then
intRowCount = DAL1.UpdatePersonalDetails(x)
Else
intRowCount = DAL1.insert(x)
End If
Catch ex As System.Exception
Throw ex
End Try
Return intRowCount
End Function
End Class
Dal1.vb
Public Shared Function insert(ByVal ParamArray I() As Object) As
Integer
Dim cmdObj As New SqlCommand
Dim intRowCount As Integer
Try
' Dim strSQLinsert As String = "insert into mk
(id,name,date1) values ('" & I(0) & "','" & I(1) & "','" & I(2) & "')"
Dim strSQLinsert As String = "insert into mk (id) values
('" & I(0) & "')"
cmdObj.CommandText = strSQLinsert
cmdObj.CommandType = CommandType.Text
cmdObj.Connection = DAL.GetDBConnection("07")
intRowCount = cmdObj.ExecuteNonQuery()
Return intRowCount
Catch ex As System.Exception
Throw ex
Finally
cmdObj.Connection.Close()
cmdObj.Dispose()
End Try
End Function
End Class
—————–
flex code :
—————–
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<!--WEBSERVICE-->
<!--WEBSERVICE - SUBMIT -->
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.soap.LoadEvent;
import mx.collections.ArrayCollection;
import mx.managers.CursorManager;
import mx.utils.ArrayUtil;
import mx.rpc.soap.WebService;
private var p1:Array;
public function SubmitUseWebService():void {
CursorManager.setBusyCursor();
WS.addEventListener("load", SubmitLoadHandler);
WS.loadWSDL();
}
public function SubmitLoadHandler(event:LoadEvent):void {
p1.push(int(userid.text));
p1.push(Name.text);
p1.push(mDOB.text);
Alert.show(p1[0]);
WS.Addsample(p1);
}
public function SubmitResultHandler(event:ResultEvent):void {
var resultObj:Object = event.result as Object ;
var updated:Boolean = resultObj.SUCCESS ;
if (updated == true) {
Alert.show("Records Updated Successfully");
CursorManager.removeBusyCursor();
} else {
Alert.show("Records NOT Updated");
CursorManager.removeBusyCursor();
}
CursorManager.removeBusyCursor();
}
public function SubmitFaultHandler(event:FaultEvent):void {
Alert.show( "fault: "+ event.toString());
CursorManager.removeBusyCursor();
}
]]>
</mx:Script>
<!-- create Webservice -->
<mx:WebService id="WS" wsdl="http://localhost:1474/test/Service.asmx?
WSDL" useProxy="false"
fault="Alert.show(event.fault.faultString), 'Error'">
<mx:operation name="Addsample" result="SubmitResultHandler(event)"
fault="SubmitFaultHandler(event)"/>
</mx:WebService>
<mx:TextInput x="191" y="56" id="userid"/>
<mx:TextInput x="191" y="86" id="Name"/>
<mx:Button x="257" y="168" label="Submit" click="SubmitUseWebService()"/>
<mx:Label x="75" y="58" text="ID"/>
<mx:Label x="75" y="88" text="Name"/>
<mx:Label x="75" y="118" text="DOB"/>
<mx:DateField x="191" y="116" id="mDOB"/>
</mx:Application>
thanks
regards
gan