Sat Mar 6 02:43:56 2010
AMFPHP is great if you want to connect to a database, mainly because the data you send is converted to binary making it's probably the fastest way to get your data from A to B. Below is a simplified explanation of how to get connected and I have included a simple class to conveniently call a connection anytime you want to send or receive data.
First of all, you will have to go to the AMFPHP Webpage and download the necessary files. (There should be a download link on the top left of the webpage). Once downloaded and unzipped, I generally change the name of the folder to 'amfphp' (if it's not already) as the version number that sometimes gets added to the name can be a little hard to remember, especially if you are working with multiple servers. However, if you think you can remember it, then you don't have to change anything. It's just something I do. You will, however, have to put this folder on your server. For this example, I will be assuming you have placed it on the root of your server, so in other words "http://yourdomain.com/amfphp".
Next up, click here to download my AMFConnect files. Included in this folder is a file called TestService.php. You will need to upload this file to the 'services' folder inside the amfphp folder you uploaded earlier. If you then go to "http://yourdomain.com/amfphp/browser/index.html you will confronted with the AMFPHP service browser where you can see all your goodies in one place. Find the TestService in the list on the left and click it. You will see a description of the testFunction that the class TestService contains. Below is a 'Call' button. Clicking will call the function testFunction and you should see "Function is working" displayed in the output window at the bottom. Any services you write in the future can be tested in this window.
Writing a AMFPHP Service
The following class is an example of a simple service that can be used with AMFPHP.
You can use it as a template for other services you write simply changing the word MyService
to the name of the file, and changing the function myFunction
to any name you want. You can have as many functions in a service as you like, all of which will be viewable in the AMFPHP service browser talked about earlier.
<?php
class MyService {
/**
@desc Write your description here
*/
function myFunction(){
return "Hello World";
}
}?>
Connecting ActionScript to a Service
In ActionScript, all we really need to do to connect to AMFPHP is the following few lines of code.
var netConnection:NetConnection = new NetConnection();
netConnection.connect("http://localhost/amfphp/gateway.php");
netConnection.call("MyService.myFunction",new Responder(onRead, onFault), sendData);
On line 1 we create a NetConnection, which will allow us to connect with the world outside of the swf file that will be created upon compilation. We then pass the url of the place we want to connect to using the connect(url)
method of NetConnection shown on line 2. Finally, on line 3 we use the call()
method to initialize the connection passing in a few parameters.
Before I continue to explain what parameters go where, it's important to know that NetConnection can be used for a number of purposes and the following explanation is specific to connecting to AMFPHP.
Continuing on, to connect using the call()
you have to pass in the name of the class and method you want to call in the form of a string. As show in the example, we can call "myService.myFunction". So if we want to connect to the TestService we uploaded and call it's testFunction, we would pass "TestService.testFunction". Next we have to pass in a Responder
. As the name suggests, this will respond to our call and tell us whether is was successful or not. You can create a responder in a separate variable, but I have chosen to pass it directly into the call method for simplicity's sake. The responder will need to receive two functions. The first function it receives will be called on a successful connection to AMFPHP, and vice versus, the second will be called on a failed connection. Most people call these function onRead
and onFault
. In the example, theses functions have not been created, but I'm sure that if you are reading this, you will know how to do that. Alternatively, you could pass in two inline functions like this:
new Responder(function():void{/*do this on success*/}, function():void{/*do this on fault*/})
Finally, the last parameter that we can pass in is data to be sent. I generally like to pass an Object
or an Array
because you can send multiple sets of data in one call and easily retrieve them in the PHP files. That being say, you could simple send as little as a String or a Number. I suppose that depends on what you want to do.
Connecting the Easy Way Using My AMFConnect Class
I have to say that this is a toned version of a similar class that I created for personal use for all my connections. Admittedly, I don't feel comfortable sharing all that hard work and plus I think it helps your own understanding to be forced to create some of this stuff for yourself. But I would like to give you a big push in the right direction, so I have included a convenience class to get you more than started.
Inside the AMFConnect folder you downloaded earlier is a class called AMFConnect to do all the connection work for you. Simply change the url inside the connect call to the location of your amfphp folder on your server.
The class requires two arguments. The first is the class and function you want to call in the form of a string. The second is data to be sent.
To use it, first import AMFConnect
and then call the following method.
var sendObj:Object = { data : "Send Data" };
AMFConnect.connect("TestService.testFunction", sendObj);
// traces Send Data:Received Successfully
If you don't pass sendObj, the NetConnection will not send any data
MFConnect.connect("TestService.testFunction");
// traces Function is working
So there you have it. It's pretty easy to send a receive data using AMFPHP. If you are having trouble, you may need to look around for information on crossdomain.xml, which I won't go into here but should solve the majority of your problems. Anyway, for those just starting out in this area, I hope this has helped.