Avg. Rating 3.8

Problem

I would like to start using AMFPHP, to connect my Flex applications with PHP and MySQL database. I cannot find one simple example with getting and sending the data to the server. It would be nice to have it all in one place so that I can use this example to create more advanced examples.

Solution

In this example I showed how to call a simple AMFPHP service method and how to handle this data in a Flex application. There is an example how to handle PHP data class instances in Flex by mapping PHP class with Actionscript class.

Detailed explanation

 

Before you start using this example, you have to download and install AMFPHP. Here is the homepage of this project - www.amfphp.org 

After you download AMFPHP, extract all files into your selected directory on your web server. For example, I created 'amf' directory on my web server. If you are using local server, you can call AMFPHP by entering URL: http://localhost/your_directory/ . In my example, I created virtual host called amf, so you can do that as well.

To test if AMFPHP is properly installed you should enter URL to the AMFPHP on your web server and call gateway.php script. You should get a message that AMFPHP is properly installed and that you can start AMFPHP browser in which you can see all PHP web services that you can call from you PHP server.

Then download the archive that is attached to this article and follow simple archive instructions.

 

First, create a database, then copy PHP files on your web server and after that import my example of Flex project. This should be a piece of cake for you. In my examples I created a simple  FlexAMFPHP.php class that contains two simple methods for calling from Flex application:

FlexAMFPHP.php

<?php

class FlexAMFPHP {

    /**
     * dummy method that can be used for testing communication between Flex and AMFPHP
     * @returns a string saying 'Connected to AMFPHP!'
     */
    function communicationTest()
    {
        return 'Connected to AMFPHP!';
    }

    /**
     * another dummy method that can be used for testing communication between Flex and AMFPHP
     * @returns a string saying 'Another method is checked!'
     */   
    function checkAnotherMethod()
    {
        return 'Another method is checked!';
    }   
   
}

?>

As you can see, PHP script must have the same name as the class inside of it. Also, I created two subdirectories, one for data objects and other one for PHP service classes.

Note: Depending on your server security preferences, you may have to add crossdomain.xml file into AMFPHP directory on server:

crossdomain.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
 <cross-domain-policy>
   <allow-access-from domain="*" />
 </cross-domain-policy>

Note: This is a basic example how to create crossdomain.xml, for more information check more how to configure crossdomain.xml

In another PHP service class you will have a simple methods for getting and receiving data. In my example I created SoccerManager class in which you can get information about several soccer teams and team players and how to update team data.

Team and player data will be stored in PHP data objects, Team.php and Player.php:

<?php
class Team {
 
 public $id;
 public $title;
 public $league;
 
 var $_explicitType = "org.zgflex.Team"; // represents a class package path in ActionScript

}
?>

<?php
class Player {
 
 public $id;
 public $name;
 public $team_id;
 public $team_title;
 public $team_league;
 public $position_id;
 public $position_title;
 
 var $_explicitType = "org.zgflex.Player";

}
?>

In SoccerManager.php class you should configure your database parameters so that you can query your database. For testing if the parameters are valid, you can try your AMFPHP service by using AMFPHP browser that you started during AMFPHP installation.

 

Note: In this example I try to show how simple it is to create a AMFPHP service and my focus was not on PHP security, SQL injection etc. So you can implement more secure PHP-MySQL functionality to prevent that kind of problems.

 

Next we need to import my finished Flex project.

 

You need to configure services-config.xml file, and edit your URL to the AMFPHP gateway.php script. You have to use this XML file while the Flex project is compiled. You will to this by adding compiler parameter in project properties. Your compiler propetries should look something like this:

 -locale en_US -services "services-config.xml"


Now you can test your Flex project. MXML application uses two remote objects, one is calling dummy PHP service and the other one works with the soccer data and helps you handle the data. Just as you created PHP data class, you need to create a simple mapping class in Actionscript. In this class you will store all the data and use it for handling data changes and sending the data back to the server.

 

StartingWithAMFPHP1.zip
[Extract this archive. Use SQL script to generate objects in AMFPHP MySQL database. Extract all from PHP directory into AMFPHP service directory. In FLEX directory you can find a finished Flex project that you can import into your Flex workspace.]
Report abuse

Related recipes