Showing posts with label amfphp. Show all posts
Showing posts with label amfphp. Show all posts

Wednesday, May 20, 2009

Using Flex and AMFPHP without a services-config.xml file

I've been working a bit with amfphp, sending data between Flex and PHP using the RemoteObject class. Initially I put the generic services-config.xml file in my application's src directory:
<services-config>
<services>
<service id="amfphp-flashremoting-service"
class="flex.messaging.services.RemotingService"
messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="amfphp">
<channels>
<channel ref="my-amfphp"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://localhost/amfphp/gateway.php"
class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
And changed my project properties to add this line to the Flex Compiler page:
-services services-config.xml

But I just came across this post which shows how to get around having to have an XML file by defining the channel at runtime.

So here is my simplified Flex code:
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.remoting.RemoteObject;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;


var url:String = "http://localhost/amfphp/gateway.php";
var channel:AMFChannel = new AMFChannel("my-amfphp", url);
var channelSet:ChannelSet = new ChannelSet();
channelSet.addChannel(channel);
var ro:RemoteObject = new RemoteObject("amfphp");
ro.channelSet = channelSet;
// Specify the PHP class
ro.source = "TestService";
ro.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void {
    trace("Fault: " + event.fault);
});
ro.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void {
    trace("Result: " + event.result);
});
// the TestService class must have a public helloWorld function
ro.helloWorld();

You could also do the same in MXML:
<mx:ChannelSet id="channelSet">
<mx:channels>
<mx:AMFChannel uri="http://localhost/amfphp/gateway.php"/>
</mx:channels>
</mx:ChannelSet>
<mx:RemoteObject source="TestService" destination="amfphp"
channelSet="{channelSet}" id="remoteObject"
fault="trace('Fault: '+event.fault)"
result="trace('Result: '+event.result)"/>

And the corresponding PHP code (put the php file inside your webroot/amfphp/services directory):
<?php
class TestService {
    public function helloWorld() {
        return "Hello from PHP";
    }
}
?>

So what are the benefits of this approach? Well, I'm not sure. In my case it was better because I have one logging class that does all the remoting, and is used by many different Flex applications. So instead of having to duplicate the services-config.xml file in every flex application this way I could define my endpoint url (http://localhost/amfphp/gateway.php) in one place.

Here is another site that is useful when getting started with amfphp:
  • amfphp video tutorials