4

I am having a problem passing an array variable from Flash (AS2) to PHP. In action script I have several arrays defined like this

output["px1"]
output["px2"]
output["px3"]

and then I use the following code to pass the variables into a php file

output.sendAndLoad("orders/print2cart.php",output,"POST");

I want to know how to get the data from the array in PHP. I have tried using $_POST['px1'], $_POST['output']['px1'], $_POST['output'] but I cannot seem to get any data. Any ideas as to what I can change to get the desired result?

Thanks!

EDIT: Just noticed that I one of the other variables in output (output.username) is also not being sent to PHP, despite it showing up in flash. Using the following code to alert to flash and it does show all the variables correctly. getURL("javascript:alert('Print Stamp: " + output.PrintStamp + " User: " + output.username "')");

EDIT: Seems like once I send a pretty long array (or a string for that matter) none of the other fields associated with the LoadVars variable are sent either. I googled it up for limits and it says text limits are ~ 63000. Still not sure if that is the problem

hakre
  • 193,403
  • 52
  • 435
  • 836
Farax
  • 1,447
  • 3
  • 20
  • 37
  • `print_r` or `var_dump` the $_POST; if the data is getting through at all, this should show you where it is. If it isn't there, try looking at $_GET just in case it's not actually doing a POST. – El Yobo May 20 '11 at 07:17
  • @El Yobo: Tried the $_GET but did not get any value – Farax May 20 '11 at 11:00
  • have you checked your server logs to ensure Flash is actually making an HTTP request? You could also use a tool like the HTTPFox Firefox extension, or a proxy like Fiddler, to watch the request traffic and inspect the contents. That should give you plenty to go on. – Paul Dixon May 21 '11 at 07:35
  • Paul, the problem seems to be in the huge array which is actually the pixels of an image. As soon as I put the array in the loadVar variables, nothing is transferred to the PHP file. After a lot of googling, I came across several other ways to pass large arrays including a serializer class, amfPHP and others. But I was hoping for an easier fix? – Farax May 21 '11 at 12:59

3 Answers3

3

Try it as a String.

Use Array.join(); in flash and send the value returned by that, then use explode() in PHP convert it back to an array.

var dataOut:LoadVars = new LoadVars();
var dataIn:LoadVars = new LoadVars();

dataOut.info = your_array.join("#");

vars.sendAndLoad("url", dataIn, "post");

dataIn.onLoad = function(go:Boolean):Void
{
    if(go)
    {
        trace('success');
    }
    else trace('connection failed');
}

The PHP:

<?php
    $str = $_POST["info"];
    $myarray = explode($str);
?>
Marty
  • 39,033
  • 19
  • 93
  • 162
  • changed the code to the following but it did not work : `code` output.PrintStamp = PrintStamp; output.username = curUser; output.abc = output["px100"].join("#"); output.sendAndLoad("orders/print2cart.php",output,"POST"); `code` IN PHP -------------- $File = "YourFile.txt"; $Handle = fopen($File, 'w'); $Data = sizeof($_POST['abc']); fwrite($Handle, $Data); fclose($Handle); – Farax May 20 '11 at 10:30
1

You can do the same as you would do with HTML, by naming your parameters "array[0]", "array[1]", etc... :

    var urlVariable:URLVariables = new URLVariables();
    urlVariable["phpArray[0]"] = "arrayEntry0";
    urlVariable["phpArray[1]"] = "arrayEntry1";

    var loader:URLLoader = new URLLoader();
    var request:URLRequest = new URLRequest("http://yourserver.com/phpScript.php");
    request.method = URLRequestMethod.POST;
    request.data = urlVariable;

    loader.load(request);

then serverside you can verify the result received by php script :

    print_r($_POST);

it should output :

    Array
    (
        [phpArray] => Array
            (
                [0] => arrayEntry0
                [1] => arrayEntry1
            )
    )

and for multiple dimension array you can use :

    urlVariable["phpArray[0][0]"] 
DzzD
  • 11
  • 1
1

Since there were no other alternatives and I went through a lot of stuff before finally concluding that Arrays of large sizes cannot be passed through from AS2 to PHP very easily. My array was actually an image converted to pixels, so what I did was that I split the array into 2 pieces and posted to the PHP file twice instead of only once. Another alternative would be to split and post the array to a text file first and then read that text file directly from PHP.

Farax
  • 1,447
  • 3
  • 20
  • 37