Table of contents
On Drupal side, use Services module you should, young padawan !
On Drupal side, use Services module you should, young padawan !
Introduction
In this tutorial we will create a simple Flash application. It will return some data about a registered user from our Drupal site using a specified email. Get Drupal 6.x from http://drupal.org/project/drupal.
We will use Drupal module Services (http://drupal.org/project/services) and AMF-PHP (http://drupal.org/project/amfphp) to provide server side communication. The AMF-PHP Module needs the AMF-PHP library. You can get the required version from http://sourceforge.net/projects/amfphp/files/

Figure 1 - Modules installation
Once you have installed Drupal and the modules (Fig. 1) you should see something like this at the address http://<YOUR BASE URL>/services/amfphp (Fig. 2)

Figure 2 - AMF PHP Service answer
So here’s how it will work:
- Our Flash application will connect to our Drupal site using the specific URL /services/amfphp
- It accesses the service which handles AMF communication. In this case this is the amfphp module, which creates a server for module services
- It calls a method on the service, mentioning a "callback" method. This method could be specified using hook_service.
- Arguments passed are serialized to AMF and deserialized at the receiving end
- The service processes the input, and optionally returns data using AMF
- The callback method is invoked by the platform (Drupal in our case) which will return data after some other actions.
Let's start doing code
OK, let's start with the Drupal part. We need to create a module to specify our custom method.
For information on how to do that, you can see http://drupal.org/node/231276. We’ll call it amfphp_test. Here's the content of amfphp_test.module:
<?php
/*
* Implementation of hook_service();
*/
function amfphp_test_service() {
return array(
array(
'#method' => 'amfphp_test.get_user_information',
'#callback' => 'amfphp_test_get_user_information',
'#access callback' => 'user_access',
'#access arguments' => array('access content'),
'#args' => array(array(
'#email' => NULL,
'#optional' => FALSE,
'#description' => t('user email.'),
)),
'#return' => 'array',
'#help' => t('Returns user information.')));
}
What do we have here? We implemented hook_service and defined the new method amfphp_test.get_user_information, which takes one required parameter with the email name and returns the result as an array. We also declare that you need content access to use this method. In our example, it means everyone has access.
For a full description of all hook_service keys see http://drupal.org/node/438416
You can also install additional service modules which provide simple methods for some modules such as node, views, user, taxonomy (Fig. 3).

Figure 3 - Other Services modules
After you do this you should see something like this at the address /admin/build/services (Fig. 4)

Figure 4 - Services module key creation
Now we need to define service callback. This will be a simple function, which will get some user information by email if it exists, otherwise it will return a failure message. Here is our code:
/**
* Service callback
*
* @param string $email
* @return array
*/
function amfphp_test_get_user_information($email) {
if ($email && valid_email_address($email)) {
$account = user_load(array('mail' => $email));
if ($account) {
$result[] = t('User id: @uid', array('@uid' => $account->uid));
$result[] = t('User name: @name', array('@name' => $account->name));
$result[] = t('User created: @created', array('@created' => date('j F Y', $account->created)));
}
}
else {
$result = array(t('Such user does not exists!'));
}
return $result;
}


