start blog post

Introduction to AMFPHP Web Services

(skip installation process)

I had my first run-in with AMFPHP this past week, and thought I'd share what I learned. AMFPHP is a technology that allows ActionScript (the programming language behind Flash and Flex applications) to talk to PHP web services. Most of the tutorials I found were for older versions of AMFPHP (current as of this post is AMFPHP 1.9 beta 20080120), and unfortunately there has not been enough backwards compatibility to get much use out of those old tutorials.

NOTE: This tutorial covers the back-end process only, future tutorials will cover writing front-end applications that connect to these types of services.

Getting Started: Downloading AMFPHP

Easy enough. Visit AMFPHP on sourceforge, click the download link under "Newest Files", and save to disk.

Getting Started: Installing AMFPHP

Step 2 is about as easy as step 1. When you unzip the file you downloaded, you will see a folder called "amfphp". Copy that folder as-is to your PHP-enabled web host.

To verify everything's fine so far, you'll want to check gateway.php in the folder you just copied. If you moved everything to http://localhost/amfphp/ then open a browser and check http://localhost/amfphp/gateway.php — you should see something like the following screenshot:

AMFPHP Default Gateway AMFPHP Default Gateway

If you click the link labeled "Load the service browser", you will see a page that lists all running AMFPHP web services. Right now, that only includes the default service which actually makes this page work: DiscoveryService.

AMFPHP Service Browser AMFPHP Service Browser

Writing Our First Web Service

Under the /amfphp/services/ folder on your web server, create a new folder called EchoService, then create a file called EchoService.php — our service is simply going to spit back any string we send it.

Directory structure for the Echo AMFPHP web service. Directory structure for the Echo AMFPHP web service.
<?php
    // EchoService.php example 1
    class EchoService {
        
        public function __construct() {
            // nothing yet, see the next example for more...
        }
        
        public function doEcho($strInput = '') {
            return "You said: $strInput";
        }
        
    }
?>

If we refresh our Service Browser, we now see EchoService listed. Go ahead and click EchoService, and you should see the name of the public function we just created, doEcho. Enter any text in the input field next to strInput, then press the Call button. This actually sends a call from the Service Browser to our new service, and displays the results. If everything is running smoothly, you should see "You said: ..." in the Results pane, along with whatever you sent as input.

Testing our new AMFPHP echo web service. Testing our new AMFPHP echo web service.

Adding Functionality

<?php
    // EchoService, example 2
    
    session_start(); // allows us to persist data across multiple calls
    
    class EchoService {
        
        var $samplePrivateVariable;
        
        public function __construct() {
            /* Any initialization can be done here, such as setting up 
             * database connections or initializing private variables.
             */
            $this->samplePrivateVariable = 'unused private variable, just an example';
        }
        
        public function doEcho($strInput = '') {
            return "You said: $strInput"; // same as example 1
        }
        
        /**
         * Every time this function is called, it silently stores the input.
         */
        public function addData($strInput) {
            if (!empty($strInput)) {
                $_SESSION['data'][] = $strInput;
            }
            return "Stored $strInput";
        }
        
        /**
         * This method echoes ALL stored input, then clears the buffer.
         */
        public function flushData() {
            $data = $_SESSION['data'];
            $_SESSION['data'] = array();
            return $data;
        }
        
    }
?>

Give it a spin in the Service Browser (don't forget to refresh first!). You'll also notice that the comments immediately before our new public methods are automatically parsed as descriptions in the Service Browser. It's a good habit to document your code for yourself as well as other developers.

Storing data across calls using PHP sessions. Storing data across calls using PHP sessions.

AMFPHP is pretty straightforward, especially for those with any Object-Oriented Programming experience in PHP. Please leave a comment to let me know if this helped, or if there's anything more you'd like to see about AMFPHP.

var tags = [, ];

  • share this post:
  • email a friend
  • float this post
  • digg this post
  • share on stumbleupon
  • submit to technorati
  • tweet this post

end blog post

most viewed this week

least viewed this week