I would like to develop a flash site with drupal as a backend, so i have installed drupals services and the module afmphp. But how can i call a service with flash. The Tutorial on the services module projekt homepage only shows how it works with flex.

I've used the following Actionscript but flash says: Error opening URL "http://muster.delooks.at/services/amfphp/"

import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;
 
var gatewayUrl:String = "http://muster.delooks.at/services/amfphp/"
 
NetDebug.initialize();
var _service:Service = new Service(gatewayUrl, null, 'view', null , null);
var pc:PendingCall = _service.getView("recipes_all", ['title']);
pc.responder = new RelayResponder(this, "handleResult", "handleError");
 
function handleResult(re:ResultEvent)
{
	trace('The result is: ' + re.result);
}
 
function handleError(fe:FaultEvent)
{
	trace('There has been an error');
}

Comments

apaderno’s picture

There is something that needs to be installed together with Macromedia Flash (Actionscript remoting components).
~Kiam

apaderno’s picture

From what I understand in your example, you have a view.php file that contains a PHP class named view, which must be in the {servicefolder} (as stated in http://www.amfphp.org/docs/creatingclasses.html). Is that what you really have?

I think AMFPHP doesn't integrate in Drupal, and that you should write some classes to integrate them.
~Kiam

delooks’s picture

AMFPHP is integrated in my Drupal installation with the amfphp module. See http://drupal.org/project/amfphp

Then there is under the administer menu in drupal a new menupoint "Services" in which i can modify(API Key Preferences,...) the classes.

Heres the Tutorial for Flex: http://drupal.org/node/118136

And now how can i call in Flash the Class view.getView()

The Flex Method:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
  <mx:Script>
    import mx.controls.*;
    import mx.rpc.events.*;
    import mx.utils.ArrayUtil;

    [Bindable]
    public var recipes:Array;

    public function init():void
    {
      getRecipes();
    }

    public function onFault(event:FaultEvent):void
    {
      Alert.show(event.fault.faultString, "Error");
    }

    public function onViewsResult(event:ResultEvent):void
    {
      recipes = ArrayUtil.toArray(event.result);
    }

    public function getRecipes():void
    {
      views.getView("recipes_all", ['nid','title','body','changed']);
    }

    public function saveRecipe():void
    {
      var edit:Object;
      if (recipes_select.selectedItem) {
        edit = recipes_select.selectedItem;
      }
      else {
        edit = new Object;
      }

      edit.type = "recipe";

      edit.title = dish.text;
      edit.body = recipe.text;

      if (edit.title == "" || edit.body == "") {
        Alert.show("Enter some content", "Error");
      }

      node.save(edit);
      getRecipes();
    }

    public function onSaved(event:ResultEvent):void
    {
      Alert.show("Recipe was saved", "Saved");
    }

    public function newRecipe():void
    {
      recipes_select.selectedItem = undefined;

      dish.text = "";
      recipe.text = "";
    }
  </mx:Script>

  <mx:RemoteObject showBusyCursor="true" destination="amfphp" source="views" id="views">
    <mx:method name="getView" result="onViewsResult(event)" fault="onFault(event)" />
  </mx:RemoteObject>

  <mx:RemoteObject showBusyCursor="true" destination="amfphp" source="node" id="node">
    <mx:method name="save" result="onSaved(event)" fault="onFault(event)" />
  </mx:RemoteObject>

  <mx:Panel width="500" height="400" layout="absolute" title="Recipes" horizontalCenter="0" verticalCenter="0">
    <mx:DataGrid x="10" y="10" width="460" id="recipes_select" dataProvider="{recipes}" >
      <mx:columns>
        <mx:DataGridColumn headerText="NID" dataField="nid" width="40"/>
        <mx:DataGridColumn headerText="Dish" dataField="title"/>
      </mx:columns>
    </mx:DataGrid>

    <mx:Label x="10" y="160" text="Dish"/>

    <mx:TextInput x="10" y="186" width="460" id="dish" text="{recipes_select.selectedItem.title}"/>

    <mx:Label x="10" y="216" text="Recipe"/>

    <mx:TextArea x="10" y="242" width="460" height="75" id="recipe" text="{recipes_select.selectedItem.body}"/>

    <mx:Button x="416" y="328" label="Save" click="saveRecipe()"/>

    <mx:Button x="420" y="158" label="New" click="newRecipe()"/>

  </mx:Panel>
</mx:Application>
brancusi’s picture

I would love to know the same.

delooks - I've poked around the amfphp browser and see that the new version (not the one they recommend to install) has an option to go between amf0 and amf3 encoding. I'm assuming unless you are using flash cs3, this is never going to work natively with the current AMFPHP version.

I really hope they do remember the as2 world.

Until then I am trying to plug the latest amfphp beta 2 into drupal.

http://www.5etdemi.com/blog/archives/2007/01/amfphp-19-beta-2-ridiculous...

Thanks

horizens’s picture

Here's a bit of code to help you out. As long as you have the AMFPHP and services modules installed correctly, this will work with Flash 8 or Flash CS3. You also have to have the remoting classes installed and then place the remoting components in your .fla library. Once you have them installed you can use the NetConnection Debugger within flash to see the results of all of the calls.

Some other things to check are to make sure that you the services module configured correctly. In Drupal, go to Site Building/Services/Settings and uncheck "use sessid" and "use keys".

You'll have to plug in your own values, but this setup works great for me:

import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;
import mx.utils.Delegate;

var gatewayUrl:String = "http://localhost/drupal/services/amfphp";
NetDebug.initialize();
var taxonomy:Service = new Service(gatewayUrl, null, 'taxonomy', null, null);
var views:Service = new Service(gatewayUrl, null, 'views', null, null);
getImages();
getLinks();
function getImages()
{
	var pc:PendingCall = taxonomy.selectNodes([17],['images','body']);
	pc.responder = new RelayResponder(this, "handleGetImages", "handleError");
}
function handleGetImages(re:ResultEvent)
{
	var rs:Object = re.result;
	var nCount:Number = rs.length;
	while(nCount--)
	{
		trace(rs[nCount].images._original);
		trace(rs[nCount].body);
	}
}
function getLinks()
{
	var pc:PendingCall = views.getView('CH_Links');
	pc.responder = new RelayResponder(this, "handleGetLinks", "handleError");
}
function handleGetLinks(re:ResultEvent)
{
	var rs:Object = re.result;
	var nCount = rs.length;
	while(nCount--)
	{
		trace(rs[nCount].field_url[0].url);
	}
}
lemmy’s picture

Hi,
I have the Flash -> amfphp connection working when connecting to a remote install of Drupal.
I can't get it to work on my local machine though, where I'm running a development version of Drupal using XAMMP.
There is always an error returned when trying to connect to a Drupal service from Flash 8:

Error opening URL "http://drupal.development/services/amfphp"

I realise this is to do with Flash's security settings, but I can't find a way to get round it. Anyone know what settings to use?

Thanks.

fever’s picture

Hi
I get the same error...

I have tried this policy crossdomain file but don't work:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <allow-access-from domain="http://www.xxxxx.com/" />
</cross-domain-policy>

For the localhost test:

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
   <allow-access-from domain="http://localhost/" />
</cross-domain-policy>

i get:

Error opening URL "http://localhost/services/amfphp"

Please help us..

horizens’s picture

How is your drupal installation used in this setup? Do you have it in it's own folder within your htdocs directory? If so, you would need to use something like this:

http://localhost/drupal/services/amfphp

lemmy’s picture

Mine seems to be working now. I think it was to do with my Apache setup.
I'm using eclipse for development, and followed the steps here for setting up a drupal development environment:
http://www-128.ibm.com/developerworks/ibm/osource/implement.html
That seemed to do the trick.

paoloteo’s picture

I got the second call (after the first one) that gives me this error but if I test them separately everything works correctly.

Anybody could help ???

Thank you
Paolo

mixey’s picture

I'm stuck with the same problem. Did you find the solution?

tinnesda’s picture

Think there is a boo boo in your code

var _service:Service = new Service(gatewayUrl, null, 'view', null , null);

Should be

var _service:Service = new Service(gatewayUrl, null, 'views', null , null); // views with an s

right?

Good luck all
David Tinnes

horizens’s picture

Also, make sure to check "access services" for anonymous users under "access control" within drupal. Otherwise flash will not be able to connect.

my mum’s picture

but that is the most helpful thing I have seen for a week - do you know of any other as2 friendly implementations/resources?

Boomshanka 'n that

anakcerdas’s picture

Hope someone here can englightened me..:P

I made this simple service and flash remote connection with AMFPHP/Drupal. The connection is ok, but in flash I can't seem to get variable that was send from the PHP service. I'm using Flash AS2.

I intend to get a game score from user who login on the website.
Here's the PHP service module: (filename is: gamestats.module)


function gamestats_service() {
	return array(
		array(
			'#method' => 'gamestats.load',
			'#callback' => 'gamestats_load',
			'#return' => 'array',
			'#help' => 'To display game score',
		)
	);
}

function gamestats_load() {
	global $user; 
	$result = db_query("SELECT gs.score FROM {gamestats} gs WHERE gs.uid = $user->uid"); 
	$som = db_fetch_object($result);
	$sc = $som->score;
	return "my score: ".$sc;
}

And here's the AS2 code:


import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;
import mx.utils.Delegate;

var gatewayUrl:String = "http://localhost/freshpoint/?q=services/amfphp"
 
NetDebug.initialize();
var gamestats:Service = new Service(gatewayUrl, null, "gamestats", null , null);
var pc:PendingCall = gamestats.load();
pc.responder = new RelayResponder(this, "handleResult", "handleError");
 
function handleResult(re:ResultEvent)
{
	textbox_ins.text = "The result is: " + re.result;
}
 
function handleError(fe:FaultEvent)
{
	trace('There has been an error');
}

In the gamestats table, there is column uid with "1" value, and column score with "1000" value.
When I'm running the Drupal Service Browser, the output can be seen. It's "1000", meaning that PHP service I made was correct.

However when I publish the AS2 file, the output is always null.

Anyone can help me out with this?

Thank You

PITRA