Tuesday, January 18, 2011

How to start with AMFPHP

AMFPHP is great piece of code made for connect Flash applications with its server side (coded in PHP in this matter) by using AMF protocol. It's way better in terms of efficiency and packet size than standard GET/POST calls. You can find installation guide here LINK.

First thing to do is to write a simple PHP class (I assume that you already have some sort of MySQL database populated with some data) to handle communication with database.

<?php

class GetData
{
  protected $connection;
  
  public function __construct()
  {
    $this->connection = new mysqli($host, $user, $pass, $name);
    $this->connection->query("SET NAMES utf8");
    $this->connection->query("SET CHARACTER_SET utf8_unicode_ci");
    
    if (mysqli_connect_errno())
    {
      trigger_error("Error - problem with connecting to database: ". mysqli_connect_error());    
    }
  }

  public function getSomething()
  {
    $sql = "SELECT myfirstcolumn, mysecondcolumn FROM mytable";
    $result = $this->connection->query($sql);
    
    if ($result == false)
    {
      trigger_error("Error - problem with getting your data.");
    }

    return $result;  
  }

  public function __destruct()
  {
    $this->mysqli->close();
  }
}

?>

If you are attentive enough you can find that I added SET NAMES and SET CHARACTER_SET commands after connecting with the database. This is because I will be dealing with polish fonts, if you are using only english language you can omit that.

You should remember that it is good practice to test every PHP function before writing any code in Flex. At this stage debugging is fairly simple thanks to great tool that comes with AMFPHP - class browser (it's mentioned in the installation guide).

Now it is time to write some code in Flex - generally you have to instantiate RemoteObject that handle all communication with PHP class.

remoteObject = new RemoteObject();
remoteObject.endpoint = "http://yourserveraddress/gateway.php";
remoteObject.showBusyCursor = true;
remoteObject.destination = "amfphp";
remoteObject.source = "GetData";
remoteObject.addEventListener(ResultEvent.RESULT, resulthandler);
remoteObject.addEventListener(FaultEvent.FAULT, faultHandler);

remoteObject.getSomething();

Remember that you can also instantiate RemoteObject in MXML, but I prefer to do this in Actionscript because I usually make my own wrapper class that keeps RemoteObject inside and only provide an interface to get data that I need without dealing with RemoteObject instances (I will try to write about this kind of class in one of my future posts). You have to remember that RemoteObject need two additional callback functions - one for result event and one for fault event.

protected function faultHandler(event:FaultEvent):void
{
  trace("Your fault: " + event.fault.message.toString());
}
    
protected function resultHandler(event:ResultEvent):void
{
  trace("Your result: " + event.result);
}

In resultHandler function you need to extract result from event.result variable. You can find very useful table about type mapping between PHP and Flash here LINK.

Bear in mind that if you didn't get fault it's not neccessary means that result is OK. Something bad could happen during processing the server code and it is your task to get information about errors in PHP and pass it in result to the Flash side. This can be done in many ways and you should decide how to deal with that. For instance you can always send back from PHP an array with information about the error (if errorCode in this array is set to 0 it is OK, if it is set to something else it means that error took place on the server side). I usually put there error code, error string and last executed SQL query.

No comments:

Post a Comment