Flex Whois Tutorial

Overview

The aim of this tutorial is to create a simple whois lookup application using the Flex 2 SDK and the AMFPHP gateway.

What you will need

How it works

Once written your Flex application will communicate with a PHP service using the Action Message Format (AMF), a binary message format designed for the ActionScript object model.
Using AMF and PHP we can expose functionality on the server making use of the many classes and libraries available to PHP (or any other language for that matter).

attachment:flexwhoisdeploy.jpg

Flex Application (*.SWF)

attachment:flexwhois.jpg

Let's start with the client as this will introduce you to the power of Flex and also provide you with an interface (it's always nice to see something as you develop).
If you have not already done so, you will need to download and unzip the Adobe Flex 2 SDK as this contains the tools required to turn your *.mxml into a very cool *.swf that can be viewed with the included debug enabled flash player. You will also find a really useful application named explorer (Adobe Flex 2 Component Explorer) that contains sample code for all the visual components that Flex 2 supports.
Before you start please take a look around the SDK folder and compile the samples using the provided scripts (view the readme.txt for more details).

The Flex Whois Application

Ok so lets get to work on the Whois client. Creating a Flex application is as simple as editing an XML document so fire up your favourite editor, paste in the XML document found Here and save it as flexwhois.mxml.
So what is this MXML file? Well basically this file contains the code that describes the user interface and how to layout the components. It also contains some Actionscript to communicate with the server but this code would normally be in a seperate file (I embedded it into the MXML to keep things simple).
If you want to find our more about MXML there is a good article Here.

So how does it work?

The main functionality of this application can be found in just a few functions.

The function whoisLookup is where the action occurs. This function takes a domain (or IP Address) as an argument and creates a NetConnection().

private var conn:NetConnection;

private function whoisLookup(domain : String):void 
{ 
    conn = new NetConnection();
    conn.objectEncoding = ObjectEncoding.AMF0;
    conn.connect("http://www.binarystor.com/amfphp/gateway.php");  
    conn.call( "whoisService.whoisLookup", new Responder(onResult, onFault), domain);
}

function onResult( result : String ) : void
{
    mainTxt.text = result;
}

function onFault( fault : String ) : void
{
    mainTxt.text = fault;
}

The AMFPHP Whois Service

class whoisService
{

    function whoisService () 
    {
        // Define the methodTable for this class in the constructor
        $this->methodTable = array(
            "whoisLookup" => array(
                "arguments" => array(
                "domain" => array("type"=>"string","required"=>true,"description"=>"Domain to lookup")),
                "description" => "Perform whois lookup.",
                "access" => "remote"
            )
        );
    }

function whoisLookup ($domain) {
    $whois = new Whois();
    $result = $whois->Lookup($domain);
    return implode($result['rawdata'],"\n");
}

Resources


CategoryTutorial