从PHP发送数组到AS3?

时间:2012-10-06 11:07:51

标签: php actionscript-3 flash

好。我正在使用此方法从PHP获取信息。

AS3:

function login_check (e:MouseEvent):void {

                    /*
                    check fields before sending request to php
                    */

                    if (login_username.text == "" || login_password.text == "") {

                        /*
                        if username or password fields are empty set error messages
                        */

                        if (login_username.text == "") {

                        login_username.text = "Enter your username";

                        } 

                        if (login_password.text == "") {

                        login_password.text = "Enter your password";

                        }

                    } else {

                        /*
                        init function to process login
                        */

                        login_process();

                    }

                }

                /*
                function to process our login
                */

                function login_process ():void {

                    /*
                    variables that we send to the php file
                    */

                    var phpVars:URLVariables = new URLVariables();

                    /*
                    we create a URLRequest  variable. This gets the php file path.
                    */

                    var phpFileRequest:URLRequest = new URLRequest("php/controlpanel.php");

                    /*
                    this allows us to use the post function in php
                    */

                    phpFileRequest.method = URLRequestMethod.POST;

                    /*
                    attach the php variables to the URLRequest
                    */

                    phpFileRequest.data = phpVars;

                    /*
                    create a new loader to load and send our urlrequest
                    */

                    var phpLoader:URLLoader = new URLLoader();
                    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;           
                    phpLoader.addEventListener(Event.COMPLETE, login_result);

                    /*
                    now lets create the variables to send to the php file
                    */

                    phpVars.systemCall = "login_check";
                    phpVars.login_username = login_username.text;
                    phpVars.login_password = login_password.text;

                    /*
                    this will start the communication between flash and php
                    */

                    phpLoader.load(phpFileRequest);

                }

                /*
                function to show the result of the login
                */

                function login_result (event:Event):void {

                    /*

                    this autosizes the text field

                    ***** You will need to import flash's text classes. You can do this by: 

                    import flash.text.*;

                    */

                    login_result_text.autoSize = TextFieldAutoSize.LEFT;

                    /*
                    this gets the output and displays it in the result text field
                    */

                    if (event.target.data.login_result == "1") {
                        login_result_text.text = "Login In...";
                        this.gotoAndStop("login_loggedin");
                    }else{
                        login_result_text.text = "Username and password doesn't match.";
                    }

                }

PHP:

print "login_registration_status=1";

并发送多个信息,如var1=1&var2=2

但是,如何将数组从PHP发送到AS3? 以及如何使用PHP中的while(数组)来处理它。

希望有人可以帮助我..

3 个答案:

答案 0 :(得分:1)

尝试类似:

<?php print 'arr=1,2,3&var1=123&var2=456'; ?>

请注意,使用PHP http_build_query生成此类字符串。

然后,在AS3中,您可以用逗号代码split

var data:Array = arr.split(',');

否则,您可以尝试使用JSON或XML来传输数据(而不是以URI格式进行序列化)。

答案 1 :(得分:0)

您可能想要使用http_build_query。例如:

echo http_build_query(array(
                'key1' => 'value1',
                'key2' => 'value2',
                'key3' => 'value3'));

将输出: key1=value1&key2=value2&key3=value3

答案 2 :(得分:0)

您可以,并且应该在敏感数据(身份验证:登录,密码)的情况下使用JSON。 在Flash Player 11中,您使用JSON,如下所示:

var arrayToSend:Array = [ 'abc', 'def', [1, 2, 3] ];

var JSONArrayToSend:String = JSON.stringify ( arrayToSend );
var arrayReceived:Array = JSON.parse ( JSONArrayReceived );

在PHP中:

$JSONArrayToSend = json_encode( $arrayToSend );
$arrayReceived = json_decode( $JSONArrayReceived );

在Flash Player 10及更早版本中,您将需要此库:http://code.google.com/p/as3corelib/