[Transmitted]PHP and FLEX communication

[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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值