Requirements

Prerequisite knowledge

It will be helpful to read through Part 1 so you’ll have an idea of how to use the data-centric development wizards in Flash Builder 4. Beginning knowledge of Flex and PHP is also helpful but not required.

User level

Beginning

In Part 1 of this series I covered some of the new data-centric development features in Flash Builder 4. I showed how Flash Builder 4 can help you bring any XML document or other HTTP REST service into your Flex application. In this article I'll show you how Flash Builder 4 makes it easier to use Flash Remoting to connect your Flex and PHP application.

In Part 3, I will provide an overview of some of the advanced data features in Flash Builder 4 for implicit paging and data management, which you can use to make your applications more intuitive and more robust.

What is Flash Remoting?

When you’re using XML 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 both the beginning tags and the end tags. XML is a great format but there is a great deal of overhead when you think of raw size.

Action Message Format

Action Message Format (AMF) works over HTTP like XML but instead of sending lots of extra information, AMF essentially strips out the extra characters to a bare minimum, or serializes it, into a binary format that uses much less bandwidth and space than XML does for the same data. This becomes very helpful 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; it's 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 AMF you have to be working with PHP objects. 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.

Flash Remoting and the Zend Framework

Recently 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 very 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. 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.

Your first Flash Remoting PHP application

Follow these steps to start creating your Flash Remoting PHP application:

  1. Choose File > New > Flex Project.
  2. Type MyFirstZendAMFApplication as the Project Name.
  3. Instead of selecting None/Other as you did in Part 1, select PHP as the Application Server Type.
  4. Click Next.

    Since you are targeting a specific language now, you need to tell Flash Builder 4 a bit about the server.

  5. Select the web root of a web server with PHP installed and then provide the root URL (see Figure 1). Leave the default Output folder and click Finish.

The PHP code

In the sample files for this tutorial, you’ll find ForestService.php and NationalForest.php.

The ForestService.php file contains all the code for a PHP service that implements the CRUD operations you'll need for Part 3 in this series.

The NationalForest.php code, shown below, is the PHP object type, which is based on the database described in Part 1.

Place both of these files in the web root of your PHP server.

NationalForest.php

<?php class NationalForest { public $id; public $state; public $area; public $established; public $closest_city; public $name; public function __construct() { $this ->id = 0; $this ->state = ""; $this ->area = 0; $this ->established = date("c"); $this ->closest_city = ""; $this ->name = ""; } } ?>

Connect to the PHP service

Follow these steps to connect to the service:

  1. In the Data/Services panel, click Connect to Data/Service and then select the PHP option and click Next.

    Note: If you don’t have an existing PHP class, you can use the data-centric development wizards to generate an entire PHP class structure based on your database.

  2. In this case you already have the PHP class, so simply browse to ForestService.php (see Figure 2).
  3. Click Next.

Flash Builder 4 will first check to see if the Zend Framework is installed. If it isn't, you will be prompted to install it so you can use Zend AMF. Note that you won't have to change your PHP file at all. Flash Builder 4 puts the Zend Framework files in the correct place and adds a gateway.php file to the bin-debug folder of the project. If you want to see how Zend AMF and the Zend Framework fit together, that's where you'll find the code that facilitates the connection between your PHP code and the Flex application using Flash Remoting.

After Flash Builder 4 introspects the PHP file, it shows you the methods that the code includes.

  1. Click Finish. Flash Builder 4 will generate the code to connect to the PHP service.

At this point you can configure the data types, create some UI components, and bind the service operations to them. The workflow is exactly the same as in Part 1.

Where to go from here

In Part 2, you saw how to connect to a PHP service using Flash Remoting. In Part 3, you’ll build on these steps to create an application that allows the user to update the database. Part 3 also covers some of the more advanced data features in Flash Builder 4 and how you can use them to make your applications more responsive and more intelligent.