This file shows you how you can connect to a Drupal service to get a list of recipes. It assumes a few things:

First that you have Drupal and all of the required modules and libraries installed:

Drupal is setup in this way:

  • Clean urls are enabled
  • AMFPHP 1.9 Beta is extracted and placed inside of the AMFPHP module, in a folder called "amfphp" (modules/amfphp/amfphp)
  • There is a content type called recipes, this needs to be created
  • There is a view called "recipes_all" which shows a list of recipes.

With the following modules enabled:

  • Services
  • AMFPHP
  • Views Service
  • Node Service
  • Views and Views UI

Services is setup with:

  • API keys off
  • Session IDs off

And your Flex project is setup:

  • as a basic project
  • with an the additional compiler argument: -services "services-config.xml"
  • and a file called "services-config.xml" in your project root, copied from amfphp.org and with the endpoint uri changed to "/services/amfphp"

Other mentions

  • You must be logged in as the administrator in the same browser you are testing Flex in to be able to save and add new posts

Here's the MXML:

<?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>

Comments

jinside’s picture

My experience is there are a couple of things you need to watch out for.

1. Do not name your flex file recipe.mxml or you will get an error saying "flex identifier and class may not have the same name". Simply rename your file to something other than recipe.mxml and you will be fine.

2. You do not need to have clean URLs installed, but you must have your drupal installation in the web root. If you do not have drupal installed in your web root you will get an error such as:

code:
Client.Error.MessageSend

Message:
Send failed

Detail:
Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Status 404

This will happen even if your services-config.xml points to the right location.

seppestaes@gmail.com’s picture

Following procedure worked for me:

1. I installed drupal and amfphp as explained in the tutorial
2. but changed the "services-config.xml" file slightly

channels
channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel"
endpoint uri="http://{server.name}/drupal/?q=services/amfphp" class="flex.messaging.endpoints.AMFEndpoint"/
/channel-definition
/channels

where "./drupal/?q=services/amfphp" is the path I copied/pasted from the browser window when testing the service in drupal.

> Administer
> Site building
> Services
> Clicked on "AMFPHP - /services/amfphp" and took a look at the path in my browser.

Didn't change
edit.type = "recipe"

Good luck!

Anonymous’s picture

If this is not working for you check which version of PHP you are using.

There is a small bug in PHP5.2.2 that you may have to fix.

See this post for more information: http://www.5etdemi.com/blog/archives/2007/05/amfphp-fix-for-php-522-comp...

Djamu’s picture

While going over the code I noticed that there's a typo in the MXML file causing the pages to be saved as a nonexistent type

edit.type = "recipe"; should be > edit.type = "recipes";

( in function saveRecipe )

While at it, how does one use this with authorization? ( keys/sessid )

jinjyaa’s picture

When I try this example, I get "Missing required arguments" from modules/amfphp/amfphp.module line 101.

I have no idea what's going on. Anyone have any insight? I've tried another Flex/AMFPHP example and got the same error. Tried both the 20080120 and 20070513 versions of amfphp-1.9.beta. Not getting anywhere. I'm not even sure what "arguments" it's referring to.

/*** LATER PS ***/

Found it! What I missed was that I had to turn Drupal service options "use keys" and "use ssid" off on page:

http://yourSiteHere/admin/build/services/settings

Now it works.

eQualitie’s picture

I was getting a duplicate entry error when updating the database because the vid wasn't being read in. So the first record it updated was fine because the vid was set to zero, a value not already set, the second and all subsequent times the error fired

Adding the 'vid' field to getRecipes solved this.

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

Also I had to locate the services-config.xml in the same folder as my mxml file for it to work.

Hope this helps someone!

Hofstadter's Law: It always takes longer than you expect, ... even when you take Hofstadter's Law into account

sskully’s picture

Hi I'm really new to drupal/amfphp/services, I'm getting this error.

Error Method view.getView does not exist????

I am able to access amfphp from flex, I used this tutorial http://groups.drupal.org/node/2768 and was able to get it to work.
I just had to change load to get.

Is it because I don't have a view set for recipes?
I don't believe this tutorial mentioned anything about setting up a view for recipes.

Are there any other tutorials for getting started with amfphp/drupal using Flash CS3 or Flex 3?
Something what would build upon the simple tutorial from groups.drupal.org that I was able to get to work?

Thanks for your help!
sskully

joachim123’s picture

views.get

method name="get"

        
vanithapg’s picture

Hi, Thank U... Ur code is very useful for me to develop web application to call the drupal database(for add and edit the drupal data using amfphp)
But, i am developing desktop application for drupal.When i create an air application with the same code, it shows me an error like "send failed"
I dot know why is it so.....

So, could u help me please...

vanithapg’s picture

Hi, Thank U... Ur code is very useful for me to develop web application to call the drupal database(for add and edit the drupal data using amfphp)
But, i am developing desktop application for drupal.When i create an air application with the same code, it shows me an error like "send failed"
I dot know why is it so.....

So, could u help me please...