Legal
The views expressed in this blog are my own and do not necessarily reflect the views of Adobe Systems Incorporated.
Search
July 17, 2006
How to pass values from Flex to a PHP backend using AMFPHP
I received a comment last week on a previous post on how to connect Flex to a PHP backend using AMFPHP. Here's how you do that:
The commenter in question was trying the following:
gateway.call( "sample.getUsers(varArray)", new Responder(onResult, onFault));
The correct way to send data from Flex / ActionScript to PHP on the backend is by sending the variable to be sent back as the third parameter in the call function, like this:
gateway.call( "sample.getUsers", new Responder(onResult, onFault), varArray);
You should be able to send back arrays as well as more simple variables: integers, strings etc..
technorati tags:flex, php, amfphp, adobe
Blogged with Flock
Comments
Careful with passing arrays - associative ones in particular. It passes the length property as well as the info.
Below is sample.php updated to use the PHP mysqli (improved) database I/O. I also added the necessary database I/O error recovery.
I also return PHP associative array (fetch_assoc(), below) to the calling flex code, where the first example sample.php returned a PHP object. The object return is unnecessary overhead.
Don't know yet how AMFPHP handles the trigger_error() calls below. What I do know is trigger_error() calls go back here, PHP4 and PHP 5 respectively:
o exception\php4Exception.php(5):
function reportExceptions ($code, $descr, $filename, $line)
o exception\php5Exception.php(33):
function amfErrorHandler($level, $string, $file, $line, $context)
If anyone can shed some light on what happens after the exception handling, I would appreciate hearing from you in this venue.
Many people using this code to send complex data to flex may find that debug mode reports 'cannot bind to xxx, class Object is not an IEventDispatcher...and the data wont bind properly or it wont work at all.
It turns out that whilst this method returns the data in the same way that HTTPService and WebService results do, with these services Flex will automatically wrap Arrays in ArrayCollection and Objects in ObjectProxy wrappers so binding will work. The solution is to manually do this using this code...
eg - returned Array has data:
return -- Array
-> [0] -- Object
->[name] = sam -- String
-> [1] -- Object
->[name] = lacey -- String
function onResult(result:Array){
for(var key in result){
result[key] = new ObjectProxy(result[key]);
}
var newAC:ArrayCollection = new ArrayCollection(result)
}
this will leave you with a fully bindable ArrayCollection!
Hope this save someone the 10s of hours I have spent on it.
I think that there is an easier way using the makeObjectsBindable property of HTTP and web services but this worked so I stuck with it!
Sam, thank you so much for posting the fix to the IEventDispatcher error. My app has been working fine, but that warning was killing me!
Thank you for signing in, You may now comment. (Sign Out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)