Introduction
We are going to setup AMFPHP + Services on Drupal (with keys!!!) and it is going to be EASY. Sound good? Any version of flash (CS2+) should work. Flex & Flash Builder also work, but more on that later. I’m going to need you to trust me on a few things. Let’s get started.
Prerequisites
Before you can use the little library I put together to make this easy, you are going to need to do a few prerequisites. This is:
1. Get Drupal 6 installed. For this tutorial I used Drupal 6.19.
2. Get AMFPHP module installed. I was using 6.x-1.0-beta2.
3. Get AMFPHP (not the module) downloaded to the AMFPHP module folder at /sites/all/modules/amfphp/
4. Get Services module (6.x-2.2 is what I had at the time of this writing
Once you have those dependencies, you should fetch and have a look at Example2 -- this archive contains the com.wwbtc.drupal library and all dependencies you need to get started.
More Prerequisities: Permissions
One thing you should probably already know to do is to go to the Admin / Modules page on your Drupal site. You should make sure that you have enabled the services module and AMFPHP. There will other services sub modules such as the node service, views service (if you have views installed), user service, etc. For the purposes of this demo you will need to also enable some of these specific services modules.
Next, go to the permissions page. You will need to enable anonymous users to have access to connect to the system and "load node data" -- these permissions options will be under their services module. Take time to look at all the stuff these services module added here on the permissions page. You want to be sure that you only enable what permissions you need for your project, and nothing more. This is just good practice for site security.
Save those permissions. If something is not right with the permissions, you will know it when you run your flash project later. Usually you will get a successful connect token back from the com.wwbtc.drupal library, but then you would get "Access Denied" or something like that on the flash console if you're having a permissions issue & you've otherwise got the stuff on your enviornment setup right.
Next Thing
Forget what you may have read in other tutorials. There is a lot of confusion around this suite of modules, how they work, etc. Some guides advocate setting things up without keys to make thing easier, but we are not going to do that. A lot of people have problems with version mismatch or bad API calls and things like that. Generally this has never been a problem for us and we have been using this library for almost a year.
[[wysiwyg_imageupload::]]
OK. Next: I, probably like many of you, like to debug on the local machine in Flash by hitting control-enter. I think it is convenient for us to be able to do this, but AMF does not allow this out of the box on production servers for security reasons. To enable the stand alone player and debugging I recommend you comment two lines from amfphp.inc in sites/all/modules/amfphp --
if (PRODUCTION_SERVER) {
// $gateway->disableDebug();
// $gateway->disableStandalonePlayer();
}
Just add the two slashes (//) in front of those two lines in the if block. When you are done developing... you’ll probably want to put those back. This is the fastest and easiest way to get the stand alone player. The AMF module and the Services module have not always agreed about how to set the debug mode for services. Theoretically the Services module controls the debug, but it doesn’t always work as intended. This is why I suggest doing it this way. This way works consistently and should not a big deal for you if you are developing Flash apps that talk to Drupal.
Moving along. You test things to be sure your environment is as expected. Go to http://example.com/services/amfphp to be sure that you get a message similar to below. Don’t worry, the links you get there probably won’t work. (If you don’t have Drupal’s URL rewrite stuff turned on, your url would be example.com/?q=/services/amfphp – or something like that.
Setting up a test key with AMFPHP
The next thing we want to do is setup a test key that can fetch a node for testing. First go to services on your site and make sure everything looks good:
[[wysiwyg_imageupload::]]
Next Click on Keys->Create Keys. You will want to fill out your application title and probably just use * for the allowed domain since we want to use this for testing from the local Flash player. You want to also be sure to check node.get and system.connect to give the service key we are creating the permission it needs to fetch nodes and connect to drupal.
Once you do that, you should be taken back to a page that gives you a long key like e88f81f1baa02c8d8654d0b009e82b35 –This key is what we will want to paste this into the Flash project shortly, so you could go ahead and copy it to the clipboard if you want. If you have been struggling with this before this tutorial, it may be important to delete other keys you have been attempting to use.
Next, click on settings and make sure you are on the general tab. Set Key Authentication in the Authentication Module dropdown, check “Use Keys” and check “Use Sessid.” Make sure that the token expiry time is 30 seconds or greater.
Note: Some versions of the module seem to have a difference in the mode shown in the UI here and how it actually behaves. It might be important to turn keys off, save, then turn them back on.
Getting started with com.wwbtc.Drupal
We are now ready to setup flash to talk to your Drupal site. You will need a couple of dependencies for your project:
import flash.net.Responder; import flash.net.NetConnection; import flash.net.ObjectEncoding; // http://code.google.com/p/as3corelib/ import com.adobe.crypto.HMAC; import com.adobe.crypto.SHA256; // http://github.com/spjwebster/as3base64/tree/master/src/com/dynamicflash/ // mx.utils.base64 is deprecated (?) import com.dynamicflash.util.Base64; // Optional, for working with serialized PHP objects // import com.wwbtc.Serializer;
I have included these in the Example2.rar to make things a bit easier but you are encouraged to examine these libraries for yourself and understand how they work. The example FLA provided has no code and only one library object—a text box named _textbox. So if you are having trouble opening the .fla you can simple create a new .fla (or Flex/Flash Builder project!) and set the document class to “Main” (as Main.as is where all the heavy lifting is) and create the textbox named _textbox and you will be all set to start testing!
Feel free to leave a comment or send a mail and let us know what you think.


Post new comment