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.
A few panoramic shots I took at SDCC 2010. #geek http://bit.ly/bwX6GB
JS version of Regex prime number checker:
function isPrime(n) {
return Array(n + 1).join("1")
.search(/^1?$|^(11+?)\1+$/) == -1;
}
Погрузился в пучину EcmaScript5, местами увлекательно, местами нудно =)
Modernizr http://ow.ly/18njQ1
A Collection of 20 HTML5 Video Players - a round-up of JavaScript and html5 alternatives to Flash-based media player... http://ow.ly/18njQ2
jQuery TOOLS - The missing UI library for the Web http://ow.ly/18njQ3
Contactable - A jQuery Plugin | the odin http://ow.ly/18njQ4
Giants vs Dodgers, sweet seats. http://twitpic.com/2ag9pa
@snookca That'll be fixed next week. I promise.
@snookca I was tryna not name names ;) But really that was just par for the course today, pretty hectic day. As I'm sure you know.
Who breaks major stuff after 4pm on Friday? On the last day of the sprint, no less. Tsk. (wasn't me)
Awesome live git tracker for teams: http://www.utsup.com/
RT @DerrenBrown: Blog post: Camera Software Lets You See Into the Past http://bit.ly/9kjVg5
10 invites to the new version of Digg: http://bit.ly/dqM8EV
Threaded vs Evented Servers, great look at the whats and whys. http://bit.ly/bDUEjn #geek
Nav, Context menus, "app-style" toolbars in sample chapter http://bit.ly/csTRY8 of new YUI book http://bit.ly/cJINoV
Add a side-mounted End Call button to your iPhone 4: http://bit.ly/cGxPBD #funny #geekAll 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!