12/16/08

Action Message Format(AMF).

Link

Design philosophy

Amfphp was designed with a few simple goals in mind:

  • Quick installation and implementation
  • Nothing required - PHP4/PHP5 compatible, no extensions needed
  • Low footprint, lightweight, fast
  • Convention over configuration (service and class mapping)
  • Can be embedded into a framework (see CakeAmfphp, Seagull)
  • Services are "non-specific" PHP classes that are portable to anything without code change
  • Productivity tools included (service browser, code gen, profiling)
  • Batteries included - XML-RPC, JSON
  • Not a framework by itself (use your own)
  • Examples
  • Mimic the AMF specification

Examples

See the showcase section for live sites running amfphp under the hood.

* File courtesy of Aral Balkan. PHP source here. Flash source in the ARP example apps.

What does the code look like in the example?

Several remote methods are defined in this service. Focusing on the getOrderList function, we have:

php
class pizzaService {
var
$ordertable = "amfphp_orders"; // the orders table
var $pizzatable = "amfphp_pizzas"; // the pizzas table
/* mysql_connect and mysql_select_db are in the constructor */
function
getOrderList ()
{
$sql = "SELECT o.order_id as orderid, o.order_status as status, o.order_name as name, p.pizza_id as pizzaid, p.pizza_details as details, p.pizza_quantity as quantity FROM $this->ordertable o, $this->pizzatable p WHERE o.order_id = p.order_id AND o.order_status=1 ORDER BY o.order_time";
return
mysql_query($sql);
}


/* Other methods below */}
?>

The ActionScript code for getting the order list looks like the following:

import mx.remoting.*;
import mx.rpc.*;

var gatewayUrl:String = "http://amfphp.org/amfphp/gateway.php";
service = new Service(gatewayUrl, null, "pizzaService");

var pc:PendingCall = service.getOrderList();
pc.responder = new RelayResponder(this, "handleGetOrderList", null);

function handleGetOrderList(re:ResultEvent)
{
var rs:RecordSet = RecordSet(re.result);
for(var i = 0; i < rs.length; i++) {
var item = rs.getItemAt(i);
//item is an object with keys orderid, status, etc.
}
}

The resource returned by MySQL query is automatically converted to the corresponding ActionScript type, mx.remoting.RecordSet. You didn't have to loop through your query, you didn't have to explode URL-encoded strings on a pipe (|) separator, you didn't have to write code to generate XML, you didn't have to use xml.firstChild.firstChild.firstChild.childNodes[i].

What if we wanted to send an argument to the remote method? Then we would simply call service.getOrderList(firstArg). firstArg could be a string, a number, an array, or an object, and we would receive the corresponding types in php. As you can see, you don't have to worry about serializing and deserializing your data, instantiating your class, or making sure the method exists before calling this, as amfphp does all of this for you. You can grab the PHP source here and the Flash source in the ARP example apps.

No comments: