27.09.2007
A new lead for amfphp

Wade Arnold will be taking the lead of AMFPHP. Thanks Patrick Mineault for all your support over the years and good look with school!

20.12.2006
amfphp 1.9 beta includes amf3, json and xml-rpc support. Read about it here.



Overview

AMFPHP is a free open-source PHP implementation of the Action Message Format(AMF). AMF allows for binary serialization of Action Script (AS2, AS3) native types and objects to be sent to server side services. AMFPHP is challenged with implementing the entire AMF protocol to be an alternative to Flex Data Services (AMF3) and Flash Remoting (AMF0). AMFPHP allows thin client applications built in languages such as Flash, Flex, and AIR to communicate directly with PHP class objects on the server. PHP developers can leverage their PHP experience in server side code development by connecting to data sources such as web-services, databases, and business applications and return that data to the client. Client applications can also offload cpu intensive methods to PHP services and wait for the result set for presentation to the user. AMF allows for native data types and complex object mapping between the client and the server. Enabling ActionScript and PHP developers to not worry about type casting between the two languages. AMFPHP is a great solution for simple data requests such as a datagrid population from a database to complex RIA's that use Cairngorm, ARP, and other MVC frameworks. AMFPHP is one of the fastest client server communication protocols available to Flash Player developers because communication is serialized into this binary format, which is generally more compact than other representations, such as XML. In addition AMF3 available in ActionScript 3 also compresses the binary communication for increased performance.

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.

SourceForge.net Logo

Beyond Carpet Cleaning - Elite Furnace Cleaning - Edmonton Furnace Cleaning Company

 


� amfphp.org | Disclaimer | Conditions of use