views:

2188

answers:

11

What is the best method for communication between Flex and PHP?

In the past, we used AMFPHP with AS2, and it worked great for the most part (advantage of AMFPHP is that it also has a JSON mode that can let you seamlessly use the same remote PHP with either Javascript or Actionscript frontends).

However, it seems like AMFPHP isn't realy maintained anymore. So what do people recommend to replace it? So far, what I've found is:

  1. Zend_AMF (looks too complex for us, we're not using the Zend framework otherwise)
  2. AMFPHP (there were some updated made to support Flex, and it seems fairly stable, but not sure on long-term support)
  3. XML (AS3 has nice XML handling routines, but it's more of a pain on the PHP side)
  4. WebORB (I have no experience with this)
  5. Roll-our-own using JSON or some other data-to-text serialization system (php's serialize(), XML, etc etc)

Mostly I'm leaning towards AMFPHP, even because of the downsides, since that's what I'm used to. Any reason I should consider switching to something else?

A: 

In all projects involving Flash and PHP backend, I worked with either AMFPHP or XML requests.

AMFPHP really simplifies understanting the application for future maintenance, although it ties the whole thing to that specific technology and involves some additional overhead on the server side - to create all needed classes.

As per XML, well, what you gain here are standard REST webservices and it doesn't depend on Flash (you could pull data from a desktop app as well, for example, whereas using JSON or any other technology dependent on browsers don't allow for that).

If you want 100% future "support", then I'd recommend what doesn't need any support at all: XML.

Seb
How is json browser-dependent?
rick
I'm feeling tempted to downvote anyone that mentions REST again. Flex cannot do REST at all without using a replacement for Flash's communication API.
Samuel
@rick: what I meant is that JSON requires a browser to work. If you're working with a desktop application, then you'll have problems accessing that webservice.
Seb
@Samuel: do you mean Flex cannot make a simple request to get an XML then parse it, and post back? I don't believe that...
Seb
Of course it can do that, what it cannot do is REST. A simple HTTP request is not REST.
Samuel
AFAIK, REST is stateless HTTP requests, which Flex / Flash should be able to handle perfectly - http://en.wikipedia.org/wiki/REST. Are you saying Flex cannot HTTP GET, POST, PUT, DELETE, etc?
Seb
Correct, Flash (which flex is based on) cannot use PUT or DELETE, only GET and POST
davr
+1  A: 

I can't tell you what's best (because that's probably somewhat subjective anyway), but what I can do is tell you about a recent project of mine.

Since this was a very rich web app, and data requests to the server would be frequent, I wanted to make sure the size of the requests were as small as possible. This mean choosing JSON as the format.

Next, becuase of the nature of the application and the fact that my flash/flex developers were 1000 miles away, I needed an API that was simple and stateless. This ultimately led us to HTTP + REST.

So, the communication layer of my app is a simple Zend Framework powered set of REST resources with URIs like

user/10
review/15
location/8/reviews

They all return JSON. There's a common JSON format for all errors, as well (exceptions are trapped and converted into JSON objects) so that the flash client can easily handle failure.

Peter Bailey
davr
You can always work with gzip'd contents, so perhaps you might want to stick to JSON - which is easier to debug - AMF often requires a web proxy such as Charles for debugging.
Seb
I'm feeling tempted to downvote anyone that mentions REST again. Flex cannot do REST at all without using a replacement for Flash's communication API.
Samuel
@Samuel: Sora. I really should say REST-like. We trap exceptions on the server and convert 500s, 404s, etc into JSON error data and force a 200 response. Not understanding HTTP responses is flex's only major REST weakenss: http://blogs.adobe.com/kiwi/2006/07/making_http_calls_in_actionscr.html
Peter Bailey
You forgot you cannot make actual RESTful calls without using a proxy, which is a huge weakness.
Samuel
Can you clarify that point for me? To the best of my knowledge, the flash/flex half of the application I mention doesn't use a proxy at all.
Peter Bailey
He's probably talking about PUT and DELETE methods, which many REST API's require, and you cannot send them via flash (only GET or POST)
davr
+2  A: 

ZendAMF Good short read - http://theflashblog.com/?p=441

For me this is no brainer. The Zend framework is one of the best php frameworks out there, and now you can talk to Flash clients. Top it off with Adobe support, that's a done deal in my book.

Alternatives :

WebORB for php http://www.themidnightcoders.com/products/weborb-for-php

AMFPHP http://www.amfphp.com If you read the url above, you'll probably know why this is no longer on my radar.

jerebear
+6  A: 

If you want to have fast and efficient communication, I highly recommend sticking with an AMF protocol instead of a REST or JSON custom format.

ZendAMF is actually not very confusing. Watch the introduction tutorial on GotoAndLearn, it's quite simple.

And just so you know, some of the developers from AMFPHP moved to work on ZendAMF. So in a sense, ZendAMF is the continuation of AMFPHP.

St. John Johnson
A: 

"If you want to have fast and efficient communication, I highly recommend sticking with an AMF protocol"

And if you want a fast, efficient, and generalized communication, go with json. Then your web service will be available to flash, ajax, or regular http requests.

rick
A) This should be a comment to John's answer.
Samuel
B) AMF is serialized to binary not ASCII, so it will be faster and more efficient. And AMF allows you to pass strongly typed objects back and forth between your backend and frontend, automatically. I'd like to see JSON do that
Samuel
The truth is that performance differences between JSON and AMF are negligible. I didn't claim json was faster, just fast. Any php object can be expressed with JSON. Php is dynamically typed. Explain what you mean by "strongly typed objects".
rick
I'm not arguing or supporting John's answer, so a comment wasn't appropriate. I'm making an additional point that json offers something AMF does not, which may be desireable in some cases.
rick
+1  A: 

If you're not using a framework like Zend, regular ol AMFPHP is still great, if for no other reason than that it's simple. I think if you feel comfortable with it, why not go for it? The thing about the role of these AMF interfaces is that they really don't need to do too much, and what AMFPHP does have in class mapping, recordset parsing into ArrayCollection, great performance.... it even does well with XML, since it gets compressed. The service browser combined with Charles has covered me as well.

I haven't been able to make much sense of how the ZendAMF effort relates to the original AMFPHP. While I can dig, I'm just saying that in following the AMFPHP mailing list on Nabble, reading Wade Arnold's blog... it's just not entirely clear.

Ben Throop
Yeah, I've used AMFPHP since way back when, I'm just revisiting that choice, since the future of AMFPHP is not quite clear to me, and perhaps a new, better, library/gateway has been written in the years since AS3 was introduced. But right now it looks like sticking with AMFPHP is the best
davr
A: 

XML on PHP can be a lot simpler with SimpleXML.

I'd just use JSON as your returns for simple calls against your PHP api.

Rizwan Kassim
Why the downvote?
Rizwan Kassim
+1  A: 

You should consider using Zend AMF. The Zend Framework is designed to be a pick and chose framework so it is completely OK to pick a single component (in this case Zend AMF) for your application.

Zend AMF is extremely easy to use. All you have to do is specify the functions/classes you want to expose and specify class mapping to your action-script classes. Everything else is pretty much transparent.

Kevin Chan
A: 

I would definitely go for WebORB. I used it with .NET in a previous job I had and it was a joy to code with. Its ease of use and its well thought management console make it very fast to learn, and its documentation is very complete; I know it's tempting to stay with AMF just because it's what you already know, but I believe it's worth to give WebORB a try.

Take a look at this screencast for Actionscript generation with PHP, it's quite fancy.

Cheers.

Sergi
The screencast shows how to use the actionscript generation...but not why, or what advantages it gives over doing it the old way. Is it just so you get autocomplete in your code editor for remote services?
davr
davr
A: 

PHP has a pretty good serialize() function, so for a recent project I did (high scores for a game), I used Sephiroth's Serializer. It makes the serialization on Flash's side nearly as easy as it is in PHP. Serializer also deals with datatypes (unlike json/xml) like AMF.

Downside--it's not as compact as AMF, but that's nothing gzip compression can't handle.

zenazn
+1  A: 

This link is a screencast showing how to use WebORB for PHP WDMF (WebORB Data Management for Flex).

http://www.themidnightcoders.com/products/weborb-for-php/developer-den/screencasts/weborb-data-management-for-flex-and-php.html

Kathleen Erickson