Connecting Macromedia Flash and PHP
Many web developers rely heavily on a popular combination of open source software to develop and deploy their websites and applications. This combination is often known as LAMP—an acronym for Linux, Apache, MySQL, and PHP.
In this article, you will use a LAMP environment to create a PHP-based Rich Internet Application. A Macromedia Flash front end brings in data through the standard Macromedia Flash Remoting Components.
You may ask, “How do I get Macromedia Flash Remoting Components to work with PHP?” Here’s how: You will use another open source project that is gaining ground and popularity, AMFPHP. AMFPHP is an application created in PHP that can connect Macromedia Flash to data, just as Macromedia Flash Remoting does with ColdFusion, .NET, and Java environments.
This tutorial guides you through building a sample web service that is driven by PHP, AMFPHP, and a Macromedia Flash MX front end. As you build the web service, you will learn to install AMFPHP, create the web service, and build the Macromedia Flash MX application that ties them all together.
Introducing the AMFPHP Project
When building Rich Internet Applications with Macromedia Flash MX, developers often need to interact with data and information contained on their web servers. The most compelling and easy-to-use method came along with the introduction of Macromedia Flash Remoting MX. Macromedia Flash Remoting MX connects Macromedia Flash and your web application server, integrating rich Macromedia Flash content with applications built using Macromedia ColdFusion MX, Microsoft .NET, Java, and SOAP-based web services.
Macromedia Flash Remoting is a great technology. However, if your development efforts revolved around a LAMP-style environment, there wasn’t a way to integrate Flash Remoting with your project without deploying additional server software and or hardware—until now.
An open source project known as AMFPHP has built a solution for developers who need to integrate Macromedia Flash with PHP. According to the developers of AMFPHP, “Flash Remoting for PHP enables objects in PHP to become objects in ActionScript … AMFPHP takes care of all the data-type conversions, serialization, and other client-server details. This provides a great way of connecting rich media clients with data and logic living on the server.”
The AMFPHP project has replicated almost all of the basic features and functionality of Macromedia Flash Remoting MX server. If you are wondering about the name of the project, the AMF in AMFPHP stands for Action Message Format. AMF is a binary format that can be delivered over regular HTTP and is the core of Macromedia Flash Remoting. AMF is essentially a lightweight binary version of SOAP.
The AMFPHP project is currently in a development state; it has not yet reached a full 1.0 release. However, as of this writing, I have found the current release 0.5.1 to be quite robust and capable. The project also appears to be a hotbed of development activity. If you are curious, check out the CVS (content versioning system) archives for the project source files at the project’s SourceForge page. (Developers have recently added the setCredentials feature of Macromedia Flash Remoting to the latest development build found in the CVS repository of AMFPHP, although it hasn’t made it into a release yet.) If you would like to experiment with the very latest version, you need to check it out of CVS manually. Throughout this article, I am referring to the currently available release: 0.5.1.
Note: If you upgrade to the full 1.0 release, the new setCredentials feature
in AMFPHP renders the roles key in the ServerInfoService class unnecessary.
You need to remove the roles key from the ServerInfoService method
table and use the new setCredentials functionality. Consult the documentation at the AMFPHP website for more details.
The latest release of AMFPHP has some very good documentation and excellent examples. Also, more and more articles and examples have been showing up around the Internet recently as folks discover this alternative method of working with external data in Macromedia Flash.
In this article, you will learn how to get AMFPHP working through an example that is custom-tailored for the LAMP environment. The finished sample application will help you monitor the status of your LAMP system. An added bonus: You can add this application directly to the Macromedia Flash MX authoring environment as a window that enhances your development productivity.
Requirements
To successfully complete this tutorial, you should have an understanding of the terminology and concepts behind Macromedia Flash Remoting, its implementation and usage in Macromedia Flash MX, and a working knowledge of PHP. Familiarity with object-oriented programming will also come in handy.
Install the following software and files:
- Macromedia Flash
-
Macromedia Flash Remoting Components
Note: Macromedia updated the components recently to address performance issues and to fix a few bugs; check to make sure you have the latest installed.
- Download the sample files:
Note: The sample files contain all the sample code referenced in this article. Use them to examine code in its original format.
-
A LAMP system
Your LAMP system can comprise a Linux operating system (or a *NIX variation such as Mac OS X) running Apache 1.3.x, MySQL 3.23.x, and PHP 4.x. You also need shell and FTP access to your server so you can install files and run scripts.
Since Apache, MySQL, and PHP are cross-platform, Linux isn’t the only operating system on which you can deploy them. You can easily apply this article to many variants of Linux as well as to Mac OS X 10.2.x and Microsoft Windows-based operating systems running Apache, MySQL, and PHP.
Some of the PHP code in the tutorial files that accompany this article relies on shell commands that let PHP communicate with and retrieve information from the underlying host operating system. In particular, the ps command for viewing running processes has many variations. The PHP code takes this into account; I’ve tested and tailored it to work with Red Hat Linux 7.x, Mac OS X 10.2.x, and SunOs 5.6 (Solaris 2.6). However, there can still be slight differences in passed arguments, implementation, and execution of these commands on other Linux variants and even on other builds or versions of these operating systems. The PHP files should provide a good basis for those of you who are willing to dig in and customize the files. It’s possible to make them work on just about any system capable of running Apache, MySQL, and PHP.
Windows 2000 Server: As a bonus, there is support for deploying the PHP-based web service in this article on a Windows 2000 server. However, to replicate the functionality of some common utilities found on Linux and UNIX systems that are used by the PHP code in this tutorial, you must install a handful of utilities available with the Microsoft Windows 2000 Resource Kit and a few shareware and freeware utilities. You can find links to these software downloads embedded in the comments in the ServerInfoService.php file that list the additional software needed for deployment on Windows 2000 systems. All of the additional software is freely available and downloadable over the Internet.
In addition, the script should also work fine when deployed using Microsoft IIS 4 (Internet Information Server) and higher as long as you have PHP and MySQL installed and know the names and locations of the log files.
Note: If you would like to learn more about the differences between the individual operations of the portions of PHP script that differ when deployed on a Windows 2000 system versus a Linux system, take a look at this reference that compares equivalent Linux and Windows 2000 command line utilities: http://www.msi.umn.edu/llsda/ntunix.html.
There is also a resources section at the end of the article that will point you in the right direction in case you need help getting Apache, MySQL, and PHP set up on your particular operating system.
Table of Contents
- Downloading and Installing AMFPHP 0.5.1
- Creating the ServerInfoService Web Service
- The ServerInfoService Interface
- Connecting AMFPHP to Macromedia Flash
- Analyzing the ServerInfoService.php File
- Creating the Macromedia Flash MX Application
- More Challenges and Resources
About the author
A native of Fort Lauderdale, Florida, Robert M. Hall is currently a Principal with Talisman Interactive in Philadelphia, Pennsylvania. Robert's work runs the gamut from design and prepress to enterprise level architecture and development of next generation ATM machines, kiosks, coin counters and secure wireless applications. For the past six years, Robert has applied his award-winning programming and design skills in the financial services vertical, developing projects and solutions for more many top-tier financial institutions such as: CitiBank, J.P. Morgan Chase, Commerce Bancorp, USABancShares, NovaSavingsBank and others.
Robert also continues to maintain his own corporate entity, Feasible Impossibilities, through which he does additional contract work and speaking engagements. He also maintains a website http://www.impossibilities.com/, covering Macromedia Flash-related news, articles, his projects, and other items of interest.
Robert recently worked as a tech editor on New Riders' Macromedia Flash MX 2004 Killer Tips, and on Macromedia Press's release Macromedia Flash MX 2004 ActionScript 2.0 Dictionary. Additional examples of Roberts work can be found in chapters he contributed to the books Flash MX Magic and Flash Enabled, both published by New Riders. He has also written a chapter on Pocket PCs in the Flash MX Bible published by Wiley, several online articles for InformIt.com, and a past Macromedia Edge article.
In October of 2003 Mr. Hall became the acting manager for the Philadelphia Macromedia Flash User Group http://www.philaflashmmug.com/ and maintains an active role in the local Philadelphia development community.
Submit feedback on our tutorials, articles, and sample applications.