AMFPHP 1.0 Works with Flex 2 / Flash Player 8.5 Alpha

Thanks to Patrick Mineault, Renaun Erickson, Dave Wolf, and Peter Farland.

To get AMFPHP to work with the Flex 2 Alpha, you need to:

  1. Extend NetConnection just so 3 methods can be added to it
  2. Have your gateway connection extend that new class
  3. Have the new encoding set to AMF0
  4. Make your calls using “YourClassAndPackage.method”

The extended NetConnection looks exactly like this:

package
{
        import flash.net.NetConnection;

        public class NetConnection2 extends NetConnection
        {
                public function AppendToGatewayUrl(append:String):Void
                {
                }

                public function AddHeader():Void
                {
                }

                public function ReplaceGatewayUrl():Void
                {
                }
        }
}

And some psuedo code for calling your server:

// import the internal classes
import flash.net.Responder;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;

// import your custom class
import NetConnection2;

public var gateway_conn:NetConnection2;
// setup your jazz
var gatewayURL:String = "http://server.com/amfphpfolder/gateway.php";
gateway_conn = new NetConnection2();
gateway_conn.objectEncoding = ObjectEncoding.AMF0;
gateway_conn.connect(gatewayURL);
// call back functions
public function onPingResult(event:Object):Void
{
        // success!
}

public function onPingResult(fault:Object):Void
{
        // loop through fault object
}
// make a method call on
// a PHP class
var r:Responder = new Responder(onPingResult, onPingStatus);
gateway_conn.call("YourPHPClass.theMethod", r);

You can pass as many parameters you want starting from the 3rd parameter, on.

Bad news is, mx.remoting.RecordSet converted recordsets for you in Flash MX 2004 & Flash 8. Since that class doesn’t exist in Flex 2, you can either wait for Adobe to do it, do it yourself, or convert plain arrays to meaningful data.

Remoting with strong-typing… mmmmmm…

This entry was posted in Flex. Bookmark the permalink.

19 Responses to AMFPHP 1.0 Works with Flex 2 / Flash Player 8.5 Alpha

  1. jeremy says:

    I did the same thing about a month ago but also including auto converting resultset from php to arraycollection, then bind to whatever controls i want :-)

    actually you just need to create a dummy AppendToGatewayUrl() method then everything else would work.

  2. JesterXL says:

    I created the dummy method, but it didn’t work. I had to add the other 2 for it to work.

  3. Aran says:

    Jesse.

    Thanks for the info. I’ve been hangin to get remoting and flex2 talking…

    small error in your e.g. – the second callback function should be:

    public function onPingStatus(fault:Object):Void

    NOT:

    public function onPingResult(fault:Object):Void

  4. JesterXL says:

    Oops… thanks for the catch, Aran!

  5. Very cool!, work perfect!!!

  6. Andy Johnston says:

    I’m a bit new to all this flex stuff any chance of an example app to demonstrate how you implement this gear? That would rock.

  7. JesterXL says:

    Hey Andy, here’s a Flex 1.5 & Flash MX 2004 set of examples. Flex 1.5 and Flex 2 syntactically are very similiar. There only difference is my example code here doesn’t use the RemoteObject tag (which is a wrapper for some of the remoting code among other things).

  8. Andy Johnston says:

    I was having a go at porting your 1.5 example over to 2 and I was getting all caught up in flex-services.xml and various other craziness when I found this

    http://www.tweenpix.net/blog/index.php?2006/01/03/543-hello-world-en-amfphp-avec-flex20

    which seems to be setup to work with a person who has just completed the amfphp tutorial files. cool.

  9. stephane says:

    With flex 2 beta :
    i implemented your solution with amfphp and it works; but if I return the result of mysql_query($query) , the dataProvider for a DataGrid doens’t work . (works fine if I retun an array).
    This solution works fine in flash MX 8 : problem seems come from Recordset and _items …
    Any idea ?

  10. JesterXL says:

    My guess is that Flash 8 has the Recordset class to parse a query whereas Flex 2 beta currently does not.

  11. stephane says:

    any suggestion to implement this feature ?

  12. JesterXL says:

    Go here:

    C:\Documents and Settings\Syle\Local Settings\Application Data\Macromedia\Flash 8\en\Configuration\Classes\mx\remoting

    And convert the RecordSet.as file to AS3. I don’t know what is involed in converting the remoting code though to make it parse to this; maybe one othe classes in there does it.

  13. jeremy says:

    don’t go the long way to port recordset to AS3, use mx.collections.ArrayCollection instead.

    you just need a loop to combine column names and data value from amfphp result to Array then wrap it in an ArrayCollection.

  14. Stephen Tallent says:

    Has anyone been successful in getting a specific class returned using this technique? I am now using registerClassAlias instead of the old ojbect.registerClass, but no matter what, it comes back from the remote service as a generic object instead of my specific class. Is it working for anyone else, letting me know its hopefully something in my code, or is it a bug/limitation in Flex Beta 1?

    Thanks.

  15. Jignesh says:

    thanx,

    It helped me a lot………………..

    Morething, i m eager to know how AMFPHP works with Flex-2 beta +FES-2……..

    thanx,

    jignesh

  16. jignesh says:

    Jesse,

    I am bit confused with the above code.its not working for me

    can u pls clarify me what will be exectly ‘your phpclass.the methode….in below code
    —-
    var r:Responder = new Responder(onPingResult, onPingStatus);
    gateway_conn.call(‘YourPHPClass.theMethod’, r);

    thanx,
    jignesh

  17. stephane says:

    the good link here

  18. Freddy says:

    Hi there,

    anyone got an idea how to give a query from Flex 2 to an AMFPHP method? I did this with flash 8 and amfphp but have problems with flex 2 and amfphp. Do I have to use AJAX or is there another way?

    Regards Freddy