Example: Accessing a service from Flash 8
Accessing exposed services from Flash8 Professional is easy, but some basic setup is required. Below the setup is sample code scraped right out of a thread in the Services group with one minor modification: I added a bit more to getData_Result to dump the retrieved node.title and node.body as well as peppered with comments for the less-adventurous.
- Install and configure Services in Drupal. Note: I found that I had to enable Access Services to anonymous users in Admin>>UserMgmt>>Access Control. In Services config, I also turned off API key and Session Key.
- Download and install the required Flash Remoting components:
http://www.adobe.com/products/flashremoting/downloads/components/ - Launch Flash 8, create a new movie, open the newly installed libraries: Click Window>>CommonLibraries>>Remoting. Drag both components onto the stage (which adds them to the movie's library) and delete from the stage.
- Put the following actionscript code in frame 1 of your flash movie:
// Picking up a Drupal node with flash remoting
// Drupal must be set up with Services module enabled
import mx.remoting.Service;
import mx.remoting.PendingCall;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.debug.NetDebug;
// expose debugging info to clientside "NetConnection debugger" utility
mx.remoting.debug.NetDebug.initialize();
// create, position, and set params on three dynamic text fields: status, node-title, and node-body
var tf:TextField = this.createTextField("status", 10, 0, 0, 200, 200);
var ntitle:TextField = this.createTextField("nodetitle", 11, 0, 50, 200, 200);
var nbody:TextField = this.createTextField("nodebody", 12, 0, 100, 200, 400);
ntitle.html = true;
ntitle.multiline = true;
nbody.html = true;
nbody.multiline = true;
nbody.wordWrap = true;
// establish connection with remote service gateway in Drupal and specify service
// - /services/amfphp is AMFPHP in Services gateway
// - node is one of the two default, exposed services in Services module. The other is view.
var node:Service = new Service("http://sitedomain/services/amfphp", new Log(), "node", null, null);
// call service method
// - load is one of three methods on the default node service
// - pass in which node to load (hard coded "1" here for node 1)
// - optional additonal param to node.load is what fields to return (like in a view?).
// -- array of field names: node.load(1, ['title', 'body']);
// -- default is to send the entire node. This example does that.
var pc:PendingCall = node.load(1);
// set up response handler functions
pc.responder = new RelayResponder(this, "getData_Result", "getData_Fault");
// set default status text
tf.text = "no response from server yet.";
// success result handler
function getData_Result( re:ResultEvent ):Void {
// re is the object containing the response
// two properties you can count on being populated for any node are re.result.title and re.result.body
tf.text = "response:";
ntitle.htmlText = "<b>Node Title:</b><br/>" + re.result.title;
nbody.htmlText = "<b>Node Body:</b><br/>" + re.result.body;
}
// failure result handler
function getData_Fault( fe:FaultEvent ):Void {
tf.text = "error";
} - CTRL+ENTER to test the movie
The above sample calls the "load" method on the service "node" with a hard-coded parameter of "1" for "node/1". The node service and its methods (load, save, delete) are exposed by the installation of the Services module. Any other methods you wish to call need to be written a Services hook (see the Services handbook).
In a real setting, you would of course consider managing sessions and logins more restrictively.
Troubleshooting this simple example of services in Flash
- If you are receiving a 403 error on your remoting calls, or if it just does not work, be sure you allow access to the service for that user (Anonymous in this example) under Administration >> User management >> Access control. (step 1 above)
- Note that for simplicity, Access and Session Keys have been disabled in Services module settings (step 1 above)
