3 May 2011
Some experience with Flash Builder and PHP is required.
Intermediate
Flash Builder for PHP builds upon the features introduced in Flash Builder 4 to make it easy to connect to PHP servers and bring data into your Flex applications. Created in cooperation with Zend, Flash Builder for PHP brings the best of Flash Builder and Zend Studio together to streamline the process of connecting, debugging, and optimizing your Flex and PHP applications.
This is Part 1 of a two-part tutorial. In this part of the tutorial I’ll walk you through getting set up and bringing in data from a PHP server using Zend AMF. Flash Builder for PHP was optimized to work with Zend Server and you’ll get the best integration if you’re using the tool in conjunction with it. However this tutorial will walk you through the steps to set up a Flex and PHP application for any generic PHP server.
In Part 2, I’ll show you some of the advanced data features of Flash Builder for PHP, such as data management and paging, so you can add advanced functionality to your Flex application.
Before diving into the tutorial, it will help to have a basic understanding of Flash Remoting.
When you’re using XML or a similar format to send data to your client applications, much of the data you’re transferring is not absolutely necessary. Think of all of the extra characters that are in an XML document. Think of how many times you have to send the attributes and nodes, as well as the beginning tags and the end tags. XML is a great format but there is a great deal of overhead when you’re just trying to get data to your client.
Action Message Format (AMF) works over HTTP, but instead of sending lots of extra information like XML, AMF essentially strips out the extra characters and reduces the data to a bare minimum. AMF serializes the data into a binary format that uses much less bandwidth and space than XML does for the same data. This is particularly important when you're sending large sets of data. Instead of sending multiple redundant tags in XML, you're sending the smaller, serialized AMF records. Another benefit of AMF is that it's a native data type for Flash Player. As a result, Flash Player doesn't have to parse the results to pull out the information; the data is available to Flash Player as native ActionScript objects as soon as it is received from the server.
Flash Remoting uses AMF to expose objects and web services on an application server as if they were local ActionScript objects. The only catch is that to use Flash Remoting with PHP you have to be working with PHP objects and classes. The AMF gateway helps translate those PHP objects into ActionScript objects in Flash Player. As a result you can create your own typed objects in PHP and work with those exact custom-typed objects in ActionScript.
Adobe partnered with Zend to provide support for Flash Remoting with AMF in the Zend Framework. There are other projects out there, including AMFPHP for other PHP frameworks, but Zend AMF has the most up-to-date support for Flash Remoting. The Zend Framework is a robust PHP framework that can be used for some very complex projects.
For some PHP developers who just want to connect PHP with Flex it may seem like a lot to chew off. But you don't have to use the entire Zend Framework or change the structure of your project to use Zend AMF. Zend AMF relies on a small but powerful subset of the Zend Framework that can be easily integrated into any PHP project.
The easiest way to get started using Flash Builder for PHP is with Zend Server, a complete, enterprise-ready Web Application Server for running and managing PHP applications. Flash Builder for PHP will recognize your Zend Server installation and configure itself accordingly. However, you can also use any server you want. I use a version of MAMP, which includes everything you need to connect Flex and PHP and I’ll use that in this tutorial.
To get started, download the sample file for this article and unzip it. You can use the included php_demos.sql file to create the database and table used in this tutorial. If you’re using MySQL, follow these steps:
To build the database, at the mysql prompt, type SOURCE php_demos.sql; and press Enter.
Before creating the application, you’ll want to set up the server you’re going to use in Flash Builder.
The next step is to create linked Flex and PHP projects, which is exceptionally easy in Flash Builder for PHP.
The PHP project is configured first.
Flash Builder for PHP will create both the PHP and Flex projects in your workspace. The two PHP files you are going to use in this tutorial are ForestServiceTyped.php and NationalForest.php. Both of these rely on a database named national_forests, which you created in Setting up.
You probably noticed that both PHP files have been commented using PHPDoc syntax. The new version of Flash Builder for PHP uses that documentation to help it introspect the classes. In the next step, the Service wizard will use PHPDoc comments to help it determine the types for specific functions and classes so it can map those to their corresponding ActionScript classes.
You’ll see that, using the PHPDoc syntax, Flash Builder for PHP has already detected some of the types for the functions (see Figure 4).
For the getAllForests() and getForestsPaged() operations, you’ll want to define the return type.
getAllForests() in the Data/Services view.NationalForest[].getForestsPaged() method.Now that your service operations are defined, you can bind them to the user interface elements of the Flex application.
getAllForests method from the Data/Services view and drop it on the DataGrid control.
You should see your data grid loaded with the results from the database.
The next step is to make the data editable.
updateForest() in the Data/Services view and select Generate Form.
// TODO Auto-generated method sub.protected function dataGrid_selectionChangeHandler(event:GridSelectionEvent):void
{
nationalForest = dataGrid.selectedItem as NationalForest;
}
Now when you select an item it will show in the form. You can update the properties in the database by changing them in the form and clicking the update button.
In this tutorial, you learned how to connect a Flex application to a PHP service using Flash Remoting. In Part 2, you’ll build on these steps using the advanced data features in Flash Builder for PHP to make your applications more responsive and more intuitive.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License