AMFPHP Example

July 9th, 2010

Installing AMFPHP

A basic installation of AMFPHP is about as difficult as downloading the files from www.amfphp.org. Once you have downloaded the files you want to copy the amfphp directory into your apache htdocs directory. Now point your browser at http://localhost/amfphp/gateway.php. You should see a message saying you have successfully installed amfphp. Next browse to http://localhost/amfphp/browser. Here you will be able to test your PHP classes. The last thing you need to know for this basic example is the services directory in the amfphp directory. This is the directory you will add your PHP application code to. You will want to create a directory for each application and then place your application’s class in that folder. One thing to note here is that your classes name must match your filename’s name.


The PHP and MySQL

For this example I am going to display a list of sports teams in flex stored in a MySQL database as well as be able to add teams to this list with a form in Flex.
Let us start out by creating the MySQL database. Below is the SQL to create and populate a teams table.

DROP TABLE IF EXISTS amfphpexample.Teams;

CREATE TABLE amfphpexample.Teams
(
    teamID int(5) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    teamName varchar(100) NOT NULL,
    city varchar(100) NOT NULL,
    state char(2) NOT NULL
)
ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO amfphpexample.Teams VALUES(NULL,'heat','miami','fl');
INSERT INTO amfphpexample.Teams VALUES(NULL,'suns','phoenix','az');
INSERT INTO amfphpexample.Teams VALUES(NULL,'hawks','altanta','ga');



Next we need a PHP class to access and modify this data. I have named my PHP class AMFPHPExampleService and added to three methods to it. The first method is the __construct method which gets and instance of a database class. The database class Im am using is the same class I have exampled in my PHP database singleton blog entry. The next method returns an array populated with an associated array for each row in our database. The last method simply adds a new team to our database.
I then saved this class as AMFPHPExampleService.php in the amfphpexample directory in the amfphp/services directory. I also have the database class saved in the services directory. This is only for the purpose of this example; otherwise you would not want to store a utility class in this directory.

<?php

require_once('Database.php');

class AMFPHPExampleService
{
	private $db;

	function __construct()
	{
		$this->db = Database::getInstance();
	}

	function getTeams()
	{
		$result = $this->db->query("SELECT teamID AS id, teamName AS name, city, state FROM teams");
		$teams = array();
		while($row = mysqli_fetch_assoc($result))
		{
			array_push($teams,$row);
		}
		return $teams;
	}

	function addTeam($name,$city,$state)
	{
		if($name != "" && $city != "" && $state != "")
			$this->db->query("INSERT INTO teams VALUES(NULL,'$name','$city','$state')");
		return "success";
	}
}



Testing with AMFPHP

Testing our PHP is simple stuff with AMFPHP. Once again point your browser at http://localhost/amfphp/browser. You will now see an expandable amfphpexample under the available service browsers. Expand and select AMFPHPExampleService. You will now be able to test all of this class’s public methods.


AMFPHP screenshot


The Flash

Below is a simple flex application to use our AMFPHP gateway. It is composed of a panel containing a list we will populate with teams from our database and simple form to add new teams to the database. This is done using the Flex’s MXML remoteobject class. You point the remoteobject instance at the service by setting its properties. You next want to add methods to the remoteobject instance. Once this is set up, by using the remoteobject’s send method you are able to basically directly call methods from the PHP class.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()" width="371" height="373">

	<mx:Script>
		<![CDATA[
			import mx.rpc.events.FaultEvent;

			[Bindable] private var statesAbr:Array = ["AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FL","GA","GU","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MH","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","PR","PW","RI","SC","SD","TN","TX","UT","VA","VI","VT","WA","WI","WV","WY"];

			private function init():void
			{
				AMFExampleRO.getTeams.send();
			}

			private function onAddTeam():void
			{
				AMFExampleRO.addTeam.send();
			}

			private function onTeamAdded():void
			{
				if(AMFExampleRO.addTeam.lastResult == "success")
				{
					newTeam_txt.text = "";
					newCity_txt.text = "";
					newState_txt.selectedIndex = 0;
					AMFExampleRO.getTeams.send();
				}
				else
					trace(AMFExampleRO.addTeam.lastResult);
			}

			private function onGetTeams():void
			{
				teamsList.dataProvider = AMFExampleRO.getTeams.lastResult;
			}	

			private function onFault(e:FaultEvent):void
			{
				trace( e.message );
			}
		]]>
	</mx:Script>

	<mx:RemoteObject id="AMFExampleRO"
		endpoint="http://localhost/amfphp/gateway.php"
		source="amfphpexample.AMFExampleService"
		destination="amfphpexample.AMFExampleService"
		showBusyCursor="true">
		<mx:method name="getTeams" result="onGetTeams()" fault="onFault(event)" />
		<mx:method name="addTeam" result="onTeamAdded()" fault="onFault(event)">
			<mx:arguments>
                <arg1>{newTeam_txt.text}</arg1>
                <arg2>{newCity_txt.text}</arg2>
                <arg3>{newState_txt.text}</arg3>
            </mx:arguments>
		</mx:method>
	</mx:RemoteObject>

	<mx:Panel width="350" height="352" title="Teams" x="10" y="10">
		<mx:List id="teamsList" width="330" height="135" labelField="name">
		</mx:List>
		<mx:Form width="288" height="167">
			<mx:FormHeading label="ADD A TEAM" />
			<mx:FormItem label="team:">
				<mx:TextInput id="newTeam_txt" />
			</mx:FormItem>
			<mx:FormItem label="city:">
				<mx:TextInput id="newCity_txt" />
			</mx:FormItem>
			<mx:FormItem label="state:">
				<mx:ComboBox id="newState_txt" dataProvider="{statesAbr}" />
			</mx:FormItem>
			<mx:FormItem>
				<mx:Button id="addNewTeam_btn" label="add" click="onAddTeam()" />
			</mx:FormItem>
		</mx:Form>
	</mx:Panel>

</mx:Application>

Flash / Actionscript, PHP

  1.  
    No comments yet.
     
Post a Comment
  1. No trackbacks yet.
You must be logged in to post a comment.