Flash provides a great interface for customizable interactions, but with typical Flash development, it's cumbersome to edit content after publishing an application. For many Flash projects, updating content after the fact isn't important. But if you want to save data or update information in your Flash application, then you're going to want to look at having some sort external data source.
External data for a Flash application can be accomplished by simply having the flash application read cookies or text files on your web server. This bare-bones approach is appropriate for some, but such an approach is pretty low-level and susceptible to bugs. If you want an application to have any sort of ongoing maintainability, then you'll want to have some sort of CMS to house your information. Because of its flexibility and widespread use, Drupal is an excellent choice for your CMS.
This tutorial is geared towards Drupal developers who may or may not have a background in Actionscript coding. You'll need to know how to install modules and be comfortable with digging into a .module file to tweak some code.
What you'll need
- Drupal - You can use either Drupal 5 or 6, but you should probably use 6, especially if you don't have to worry about legacy code. This tutorial will use Drupal 6.
- A Flash compiler - The obvious choice is Adobe's Flash product (http://www.adobe.com/products/flash/), but you can use any flash compiler. More info on open-source flash at http://osflash.org/.
- Services Module (http://drupal.org/project/services)- This module is one-half of the glue that sticks Drupal and Flash together. The services module provides an extremely easy way to take Drupal data outside of the CMS into various different environments.
- AMFPHP & AMFPHP module - (http://www.5etdemi.com/blog/archives/2007/01/amfphp-19-beta-2-ridiculous..., http://www.amfphp.org/) - AMFPHP is the second=half of the Drupal/FLash glue. AMFPHP provides a data gateway between PHP applications and Flash.
- Text Editor - I'm currently in love with Coda, but any editor will do. (Adobe's Flash product has a text editor built in.)
Step 1 - Set up Drupal & Services module
Setting up Drupal is fairly straightforward. If you aren't already familiar with how to install Drupal on your server, there are plenty of good tutorials on the subject elsewhere on the web.
The Services module is also pretty straightforward to install. Just like any other module, throw the folder into the modules folder (either /sites/all/modules or /sites/[your-site-name]/modules), then enable the module as usual.
In addition to the Services module, you'll want to turn on some of the services that are included in the Services module. You'll definitely want to turn on the System service module. This module allows you to, among other things, pull a session ID and user item, which is going to be important later. You'll also want to turn on any of the other services you'll need. For the purposes of this tutorial, I am turning on the System and Node services.
Note on Security
The services module offers two different ways to prevent access to the service: Session ID's and keys. Key's are unchanging strings handled directly by the Services module, whereas session ID's are unique id's given by Drupal. For the purposes of this tutorial, you should turn off the keys, but leave the session id setting. You can do this by going to the settings page on the Service module.
Step 2 - AMFPHP
Next, you'll want to install AMFPHP and the AMFPHP Drupal module. The Drupal module goes into the same folder as the Services module, and the AMFPHP files will go into the AMFPHP module's folder.
The AMFPHP module is not fully compatible for Drupal 6 yet. There isn't a separate branch for Drupal 6 yet, but the HEAD release is mostly compatible with 6. There is a bug in the module that keep it from working properly. There is a patch available (http://drupal.org/node/320019), or you can just replace the first line in the amfphp_server function from
$path = drupal_get_path('module', 'amfphp');
to
$path = $_SERVER['DOCUMENT_ROOT'] . base_path() . drupal_get_path('module', 'amfphp');
Update 4/29/2009: Since writing this article, the AMFPHP module has been updated to Drupal 6, and works as is. Huzzah!
After turning on the module, you want to make sure it's working. On the main services page, click on the AMFPHP server link. It should tell you that you have everything installed properly. After that, you'll need to set up the right permissions so that you'll have access to Drupal's data. Go to the permissions page and turn on permissions to allow anonymous users to access services and load any node info.
Step 3 - Actionscript
Now that you have Drupal set up properly, all you need to do is get Flash connected to your service. Open up your Actionscript editor of choice and add use this code to get you started.
package com.thanksfornotsuing{ import flash.display.MovieClip; import flash.net.NetConnection; import flash.net.ObjectEncoding; import flash.net.Responder; import flash.events.Event; public class DrupalConnector extends MovieClip{ private var myService:NetConnection; private var sessionID:String; /** * Contructor */ public function DrupalConnector(){ myService = new NetConnection(); var myServiceURL:String = "[location of your Drupal install]/services/amfphp"; myService.objectEncoding = ObjectEncoding.AMF0; myService.connect(myServiceURL); var myResponder:Responder = new Responder(getSessID, onFault); myService.call('system.connect', myResponder); } private function getSessID(returnObject:*) { trace(returnObject.sessid); var myResponder2:Responder = new Responder(getNodeInfo, onFault); sessionID = returnObject.sessid; myService.call('node.get', myResponder2, sessionID, 1); } private function getNodeInfo(returnObject:*) { for (var item:* in returnObject) { trace(item + ": " + returnObject[item]); } } private function onFault(f:*) { trace("Error" + f); for (var item:* in f) { trace(item + ": " + f[item]); } } } }
There are two main concepts to understand about the application: the NetConnection object myService and the two Responder objects. The NetConnection class provides the ability to connect to external data sources. We use this class to connect to the Drupal AMFPHP installation. The Responder class allows us to call a function after information has been pulled from the data source.
The above code calls the 'node.get' service function and displays the node data of the first node in the console.
Going Further
Now that you have information loading from Drupal properly, you can load any piece of information you want out of Drupal. The Services module provides services to load user, node, taxonomy and view services, as well as access to search, menu, and file information. If you need something outside of these items, it's relatively simple to write a custom service, which I will go over in a later tutorial.

Comments
The services module, as it currently stands, isn't terribly efficient at pulling large amounts of data (i.e. multiple nodes) at one time. I suspect that it partially has to do with the fact that there isn't a terribly efficient way to pull multiple nodes in core just yet (Although that's coming in D7 (http://api.drupal.org/api/function/node_load_multiple/7)).
If I were you, I would either look at having Drupal generate the XML files or write a custom service call for your app. With a decent server and proper caching, the performance hit you're going to get is going to be negligible compared to a static file unless you're looking at some serious traffic (tens of thousands of visitors a day plus), and you don't have to worry about ensuring your static file is up to date with what's in the database.
Great and valuable article, I was amazed to learn this collaboration between Drupal CMS and Flash, since I dont know that much in Flash action script.
Any way, I have a drupal website and I need to interact with Flash to display a list of items retrieved from the CMS, I was thinking of doing this using XML file, whenever a node is inserted by the admin; which means a new item is added to the website, the XML file is re-written with the new data and it is structured in a way that the actionscript can read it. I will use some of the services module functions (for example when saving nodes from the flash) but I dont know how efficient is it !!!? when retrieving large number of data and items, especially when displaying hundreds of images listed in the flash; in this case I can't assume that this way is faster than XML mapping, cause as I know File System is always faster than DB. Any Advice!!!? please help me which way I follow ?
Kind regards,
Mohannad
Technically possible, yes. However, I'm not aware of any solutions that tie the two together, and I don't really know enough about the guts of Yooba to give an educated guess of how difficult such an integration would be. It would depend on how well thought out the API is for Yooba, and how well it maps out to Drupal functionality.
It is possible to integrate Drupal and Yooba.
Yooba is a Flash CMS built specifically to be integrated with existing CMS solutions.
Look at http://www.yooba.com
This is much needed when you dont't wanna use any of the javascript gallery type modules.
javascripts can be turned off and in that event the gallery won't work.
Thanks a lot for providing such a valuable information.
We are trying to follow this tutorial but we have a couple of questions:
1. We do not see an option to "Go to the permissions page and turn on permissions to allow anonymous users to access services and load any node info."
2. When we do set this does it mean that all content could be exposed to anonymous users via the services module?
Hi
where should i write this actionscript and where its has to be saved...
Almost there!
but.. at node.get or node.view etc.. I can't connect flash to drupal.. Apparentely the sessid does not be updated. Because when I copy the sessid from drupal (services) they get all content, but when I try get with connection they don't work... any idea?
ps.: system.connect is working bringing sessid.
(sorry for my bad english I'm just student from Brazil.)
Awesome, awesome article.
How would you retrieve the total number of node in the service though?
i find it have an error in amfphp.module ,so i use the amfphp.head.module, it is ok.
var myServiceURL:String = "http://fangzhi/services/amfphp";
myService.connect(myServiceURL);
var myResponder:Responder = new Responder(getSessID, onFault);
myService.call('system.connect', myResponder,"1");
}
private function getSessID(r:Object) {
trace('ccc');
....
i copy this code in my flash but the function getSessID don't work, the ccc is't output.
In trying to do this using Drupal 5, we discovered that we needed to use the method node.load instead of node.get as described in the ActionScript. Not sure why this would be... Our installation didn't understand "node.get"
Hi,
i found the solution
http://blog.richardolsson.se/blog/2008/12/character-encoding-bug-in-amfp...
Thanks
By
Hmm, I'm not sure offhand, but I'll open it up to anybody paying attention. Any takers?
Hi,
great article,
i'm a french guy and when i put "accented characters" Like "éè..." i have a problem whith the encoding the result is "é or è"
Mysql is 5 and interclassent of table and base is utf8_general_ci
have you any idea ?
Bye and thanks
Yto
Oh my, thank you. Been meaning to find a nice CMS -> Flash bridge for a while. This is so simple. Thank you!
Thank you! I'm actually wanting to follow up this tutorial with another one explaining how to use API keys. I can't guarantee it'll be out anytime soon, but the subject's definitely on my radar.
What you're describing is technically possible, but would definitely require a lot of planning to get everything working properly. I'd be interested in hearing more about your project.
Hi, I've been searching for the best way to feed flash with content. I found many posts but your's the only one that functioned to me. I am able to get a nod ID in particular, but, how could I enable API key to maximize security and how can I get all ID's I have in nodes so I can work in flash? That's the only thing that's blocking me to construct a site in flash with drupal CMS backend.
I have some knowlogde in AS3, none in php, and this is the first time I've worked with AMFPHP.
Thanks in advance.
Awesome! I'll have to update the tutorial.
AMFPHP for DRUPAL 6
http://drupal.org/node/219365
Hey Brandon,
Thanks for the great article.
One quick note: the only way I could get your example working was to supply an array of fields in the call function. I.e.,
myService.call('node.get', myResponder2, sessionID, 1);
- had to be changed to:
myService.call('node.get', myResponder2, sessionID, 1, ["title", "body"]);
Thanks again!
Use AMFPHP HEAD module.
I've gotten everything to work except for when I try to get the actionscript to "talk" to Drupal. I get this error:
Error #2044: Unhandled NetStatusEvent:. level=error, code=NetConnection.Call.Failed
at DrupalConnector()
Have you tried the As3 class from http://thirdavedesign.com/drupalsite/?
A lot of people use only Drupal 5, is it a better way for now?
I think there's also a new services module is your code still work with it?
I'm using version Services 6.x-0.13. What error are you getting from the module?
I get the following error with the latest version of the services module. Apparently this is because of the new naming conventions!!! What version of the services module did you use?
You want to use the HEAD release of the Module. The maintainer hasn't branched out a Drupal 6 version yet.
http://drupal.org/node/96669
Am i looking over the Drupal 6 AMFPHP module link? According to Drupal module site there's no version for 6 ?
Post new comment