PHP + Actionscript AMF (via SabreAMF) - How to return a PHP array and map to array / object in actionscript

StackOverflow https://stackoverflow.com/questions/14089992

  •  13-12-2021
  •  | 
  •  

Quick note : My actionscript skills are weak sauce... Im no monkey tho, so given the proper advice, I can figure things out:

So, we use SabreAMF to handle some AMF calls to our back end.

Things work nicely.

The callback server and actionscript side of things all work as expected.

The one issue we would like to resolve is:

As it stands now, when we need to return a set (array) of data we just concatenate it by doing something like this (on the php end):

return 'VALUE1|VALUE2|VALUE3';

And then on the actionscript side we do something like this:

var parts:Array=result.split('|');

/* parts[0] = VALUE1 , parts[1] = VALUE2 , parts[2] = VALUE3 */

The above works just fine, but I cant help but think there must be a cleaner way to do things.

We would like to do something as follows:

PHP End (SabreAMF):

 return array('Param1'=>'Value1','Param2'=>'Value2','Param3'=>'Value3');

And have this data mapped to an array or object automatically on the actionscript side. It would seam logical that one could return arrays and have them mapped properly (and automatically, fingers crossed) on the actionscript side.

So, the question is as follows:

When using SabreAMF to handle actionscript AMF calls to a PHP based back end, is it possible to return an array and have actionscripts AMF handler automatically map the data set to and array (ideally) or object without using concatenation?

Concatenation seams like a bit of a hack.

Thanks!

有帮助吗?

解决方案

Send it as a String via JSON using json_encode(), then in AS3 use any JSON library to convert it to proper data structure. I think it's the simplest way to do it... or maybe I'm wrong and someone will give us the better answer.

EDIT: You can also send it as XML string, though my AS3 coworker always insists to use JSON as an easier alternative.

其他提示

You can do what you want using AMF. Flash will interpret the received data transparently. You might want to check on AmfPHP though, as Sabre AMF is unmaintained. (disclaimer: I run AmfPHP).

JSON and XML work too. AMF is maybe a bit more work to set up, depending on what you know. Its main advantages are saving bandwidth and strong typing, so work out what is best for you.

http://www.silexlabs.org/amfphp/

The PHP AMF3 extension can be incorporated into your project a lot faster.

Check this out - https://github.com/neoxic/php-amf3

// Have your data
$data = array('Param1'=>'Value1','Param2'=>'Value2','Param3'=>'Value3');

// Encode into AMF3
$str = amf3_encode($data);

// Print to stdout (or use your way to pass it to the client)
print $str;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top