[Original]http://nitingore.wordpress.com/2007/07/19/php-and-flex-communication/
Communication between PHP and Flash using POST method of HTTPService service:-
Here I have created Flex project which passes two variables to PHP and get the sum back from PHP and displays the same in Flex. You need to send the variables in “name” “value” to PHP page as shown in code, same name value pair can be passed as Sum_HttpServ.send({num1:’3′,num2:’6′});
Below is the code written in Flex application:-
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”fnCreationComplete()”>
<mx:HTTPService id=”Sum_HttpServ” url=”http://yourDomain/someFolder/GetSum.php” result=”fnDiplayResult(event)” fault=”fnHandleFault(event)”/>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.controls.Alert;
private function fnCreationComplete():void{
var Obj:Object=new Object();
Obj.num1=2;
Obj.num2=5;
Sum_HttpServ.method="POST";
Sum_HttpServ.send(Obj);
}
private function fnHandleFault(event:FaultEvent):void{
Alert.show("Error ID="+event.fault.errorID+" faultString="+event.fault.faultString);
}
private function fnDiplayResult(event:ResultEvent):void{
Alert.show(event.result.toString());
}
]]>
</mx:Script>
</mx:Application>
Below is the code written in the PHP “http://yourDomain/someFolder/GetSum.php” page which is placed on remote machine in virtual directory
<?
$Number1=$_POST['num1'];
$Number2=$_POST['num2'];
print ($Number1+$Number2);
?>
To use GET method, you need to change Sum_HttpServ.method=”POST”; in flex project and use $_GET[‘num1'] and $_GET[‘num2'] in PHP page.
Getting Multiple parameters from PHP:-
To return the multiple parameters from PHP you can return the variables separated by ampersand (&) if you are using flashvars as resultFormat. There are fore more formats namely “text”, “array”, “xml”, “e4x” you can use anyone as per your need but depending on that you will have to format your response object in PHP. Here I am using flashvars as result format.
Below is the PHP code that returns addition & multiplication of variable.
<?
$Number1=$_POST['num1'];
$Number2=$_POST['num2'];
$Multiplication=$Number1*$Number2;
$Addition=$Number1+$Number2;
print “Addition=$Addition&Multiplication=$Multiplication”;
?>
And now you are ready to send the variables from PHP. Now in flex application, your fnDiplayResult() function will change to
private function fnDiplayResult(event:ResultEvent):void{
Alert.show(”Addition = “+event.result.Addition);
Alert.show(”Multiplication = “+event.result.Multiplication);
}
And add line Sum_HttpServ.resultFormat=”flashvars”; in fnCreationComplete() function before you send the parameters to PHP.
Flex与PHP通信
本文介绍了一个使用Flex与PHP进行通信的例子。通过POST方法传递变量到PHP,并从PHP获取加法运算的结果。此外,还展示了如何从PHP接收多个参数。
7406

被折叠的 条评论
为什么被折叠?



