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.
Easy enough. Visit AMFPHP on sourceforge, click the download link under "Newest Files", and save to disk.
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:
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.
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.
<?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.
<?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.
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.
I'm a Front-End Engineer at Yahoo! working on the Mail and Messenger teams. I blog about web design and development topics including accessibility, usability, performance, and developing HTML / CSS / JavaScript applications on Appcelerator Titanium and Adobe AIR.
If you're a web developer, you might enjoy Jelo, my JavaScript library.
All original work on this site is covered by a Creative Commons Attribution 3.0 license unless otherwise specified.
You may share or use any code or images from this site in any manner, for free, so long as reasonable effort has been made to give credit where due.
The views expressed in the posts and comments on this blog do not necessarily reflect the views of Yahoo!