Best method for Flex to PHP communication?
Asked Answered
B

12

8

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?

Buntline answered 16/3, 2009 at 20:57 Comment(0)
K
8

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.

Kyanite answered 19/3, 2009 at 15:22 Comment(0)
C
2

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.

Chamness answered 19/3, 2009 at 7:2 Comment(0)
O
1

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.

Osithe answered 16/3, 2009 at 21:5 Comment(7)
Thanks for the input. The advantage of AMF vs JSON is that it's a binary protocol, so it will actually be more efficient than JSON in terms of data size & encoding/decoding speed.Buntline
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.Joellejoellen
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.Selfidentity
@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: blogs.adobe.com/kiwi/2006/07/…Osithe
You forgot you cannot make actual RESTful calls without using a proxy, which is a huge weakness.Selfidentity
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.Osithe
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)Buntline
S
1

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.

Solomonsolon answered 20/3, 2009 at 4:32 Comment(1)
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 bestBuntline
N
1

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.

Name answered 23/3, 2009 at 6:44 Comment(0)
B
1

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

Bronk answered 18/3, 2010 at 14:37 Comment(0)
J
0

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.

Joellejoellen answered 16/3, 2009 at 21:2 Comment(7)
How is json browser-dependent?Heterosis
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.Selfidentity
@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.Joellejoellen
@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...Joellejoellen
Of course it can do that, what it cannot do is REST. A simple HTTP request is not REST.Selfidentity
AFAIK, REST is stateless HTTP requests, which Flex / Flash should be able to handle perfectly - en.wikipedia.org/wiki/REST. Are you saying Flex cannot HTTP GET, POST, PUT, DELETE, etc?Joellejoellen
Correct, Flash (which flex is based on) cannot use PUT or DELETE, only GET and POSTBuntline
U
0

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.

Utica answered 20/3, 2009 at 20:6 Comment(0)
C
0

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.

Chow answered 23/3, 2009 at 13:25 Comment(2)
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?Buntline
In addition, there's a slightly nicer syntax. Eg with AMFPHP & using the built in Flex remoting classes, I might do 'service.myCoolMethod.call("hello")' and with weborb 'service.myCoolMethod("hello")'...so I guess you save a little typing.Buntline
P
0

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.

Poyssick answered 25/3, 2009 at 15:42 Comment(0)
N
0

AMF has a pretty situational advantage. If you're looking to transfer large and complex Object, by all means go with AMF. But little does people know about the overhead that AMF carries when you're transferring small objects. If you're only transferring an object with 3 properties, using AMF can triple your payload size.

On a side note, I'm a big advocate of RESTful architecture. Since JSON and AMF are both just representations, you can build a REST service that accepts both, and negotiate the actual representation with your client at runtime.

Newsman answered 30/3, 2011 at 1:22 Comment(1)
AMFPHP actually has a JSON interface as well as AMF. This is what we're using (an old version of AMFPHP though, no idea what's up with newer versions)Buntline
H
-2

"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.

Heterosis answered 19/3, 2009 at 18:8 Comment(4)
A) This should be a comment to John's answer.Selfidentity
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 thatSelfidentity
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".Heterosis
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.Heterosis

© 2022 - 2024 — McMap. All rights reserved.