amfphp: setup_ part 1

This is a setup of amfphp. I'm not here to sell it, just to show you how to do it. It's something I wish I had found more easily on the web, so I thought I'd share my setup of it.

Background:

In flash / flex web development, you manipulate value objects. It helps if the client and the server agree on the same value objects. If you send an Asset Value Object from the client to the server, you want the server to agree on all its properties and send it to the database for creating, reading, updating, or deleting. (For purposes of this setup / example, I use Flex as the client, and php as the server-side code).

A value object is, simply put, a representation of an item in a database. If your database has a table called "friends" and each "friend" item in the database has "firstname", "lastname", "address" field, then a representation of a friend would be your Friend Value Object or FriendVO. If you setup a class in php and a class in Flex called "FriendVO", with the properties of "firstname", "lastname", "address", then suddenly everyone is agreeing about the same Value Object.

Why is this important? Well, if I didn't have VO representation, and added a new field to my "FriendVO" class in the database called "emails", I would have to find every instance of Creating, Reading, Updating, and Deleting in my entire codebase, for both the Flex and PHP side and modify them to include this new "email" field. BUT, if they're represented by a class object, I just need to update the two class value objects (one in Flex, the other in PHP), and now both sides can retain the same code. Also, it makes your code more organized and easier to understand for large-scale projects.

Because they both agree on the same object!

But Flex and PHP don't automatically agree on these objects...because the message sent between them is in AMF - action messenger format, a light-weight way to transmit data in machine language. AMF is great because it makes transferring data lighting fast...but it does so by removing all the wonderful semantic language you'd need to understand exactly what the object is. It trades semantic language for speed. So to get both a client (flex) and server (php / coldfusion / asp, etc.) to agree on these objects, you have to convert the message sent between them from AMF into their agreed upon classes.

Introduce AMFPHP, which takes care of this conversion or Data Serialization, for you in php. I prefer using amfphp, for a variety of reasons, but basically_

  • it's quick to setup
  • it's light-weight
  • it makes your code easy to understand by retaining class objects
  • it's easy (once you get over the small learning curve)
Step 1_ Downloading AMFPHP

Download the latest version of AMFPHP

Unzip it and you'll see the amfphp folder. You should see a folder similar to this.

Step 2_ Uploading AMFPHP & Testing for PHP

Upload amfphp to your root directory (or wherever you want to host the amfphp services). Test if you have PHP by opening your browser and going to this location on your webserver_

http_//yourwebserver.com/amfphp/phpinfo.php

You should see a page similar to this. Most webhosts have PHP 4/5 (you'll want 5) and this tutorial doesn't cover installing PHP but there's plenty of great places on the web to find out how!

Step 3_ Testing AMFPHP Gateway

Next, navigate to your amfphp gateway to confirm that amfphp is connected properly. The location is_

"http://yourwebserver.com/amfphp/gateway.php"

You should see a page similar to this.

Step 4_ Testing AMFPHP Browser & Services

Next, navigate to your amfphp browser to see a list of services built out, by either clicking the "load the service browser" link or navigating your browser to_

"http://yourwebserver.com/amfphp/browser/"

The amfphp browser is actually a Flex app that takes your settings and connects to amfphp! You'll be prompted by a Configuration Screen which I recommend you accept at default. Here's an explanation of the settings_

  • Gateway Location - The location of your gateway that Flex needs to connect to in order to pass Value Objects back and forth. It's default is your actual gateway, so if you move your gateway, you'll want to update this value to the new location.
  • Encoding - AMF0 or AMF3 - basically defines how the value objects are being encoded from Flex to be received and translated or "serialized" by amfphp. I recommend using AMF3 since there's a pretty good performance boost in speed with that kind of encoding / decoding
  • Set Tab After Call - When you test a service, you'll get a series of tabs displaying the results. This value just sets which Tab is viewed first.
All your web services are placed in the "amfphp->services" folder. The default service that comes with amfphp is "DiscoveryService" which has the methods "describeService", "getServices", "listTemplates". You can call any of these services in the Flex browser of the page and it will return results.

The List component on the left lists all your services. The panel on the right gives a detail view of each service and a button to call each service as well as a text field for any inputs the service requires. This can be incredibly helpful if you want to test out a service you've just written!

Step 5_ Testing AMFPHP Discovery Service

Navigate to the amfphp DiscoveryService and try the getServices method! Notice it displays the Method Name, Method Description, and Return Values. Press the "Call" button and you should see results in the tab list at the bottom (as off this posting, the "describeService" has been deprecated I believe). Pretty neat but boring. We need to make our own service!

Here's an example.

That'll be covered in post 2. Thanks for reading, and I hope this helps.

FTD

This article is my 39th oldest. It is 977 words long, and it’s got 1 comment for now.