Setting up Drupal for web services
From InstallationWiki
| Official Page |
| Project Documentation |
| Download |
|
In order to handle web services within Drupal, several contributors took an initiative to develop a series of fantastic modules that'll accomplish the task of remote communication. These modules are all subsets of the module Services, which can be found at http://www.drupal.org/project/services. Utilizing the power of this module, external applications are given the ability to make remote calls to Drupal and extract the data that they need. So, our first task in making a Flash application that says "Hello World" within Drupal will be to set up our Drupal installation so that it is ready for external interaction. And we will do this using the Drupal Services module.
Contents |
[edit] Installing and configuring the Services module
The first task in getting web services connected for Drupal is to install the Services module. You can find this module located at http://www.drupal.org/project/services. Once you are there, you will be given the option to download a version for either Drupal 5 or Drupal 6. This tutorial assumes you are using Drupal 6, but since the Services module works the same way on Drupal 5 as it does on 6, you should be able to follow along, regardless of which version of Drupal you are using. Once you have downloaded Services, you will need to extract the contents of this TAR file in your sites/default/modules folder, which we created in the previous tutorial. After these files are in place, you will notice that there are two subfolders within the main Services directory called Servers and Services.
[edit] Servers and Services
Services are the software elements that define the routines that can be executed by outside applications. By enabling any of these modules, you are now exposing a series of routines within their corresponding modules as "executable", so that any outside application can use them to retrieve or set data within Drupal.
The Servers act as the translators that take the XML format of the service request from an outside source, and then translate that XML into the execution of an internal routine. They also handle the responses (or returns) from those routines, and convert them back into the XML format to be sent back to the external application that originally made the request. There are several different types of servers that can be used for RPC communication, but the one that we will need to use for our ActionScript 3 interaction is called AMFPHP (Action Message Format PHP).
[edit] Installing AMFPHP
AMFPHP is a remoting gateway to be used specifically for Flash and ActionScript, and Drupal already has a module that creates an AMFPHP server plug-in for the Services module. This can be found at http://www.drupal.org/project/amfphp. Since this is a server that will be used with the Services module, we can place the contents of this download within the servers folder in the services module directory.
But before we install this module, we will need to make sure that we download the AMFPHP source files located at http://www.amfphp.org. Once we have downloaded the AMFPHP server package, we will need to place the contents of that package within an amfphp folder in our AMFPHP module folder as shown in the following illustration:
After we have both the AMFPHP and Services modules in place, we can then navigate to the Administer | Modules section of your website and take a look at the services section, which includes the Services module, servers, and services.
Although we will explore and utilize most of these services in this tutorial, in this tutorial we will only be concerned with enabling the System and Node Services. These services will allow us to log in to Drupal and extract node information for any given node ID in the Drupal system. With that said, let's go ahead and enable the Services, AMFPHP, System Service, and Node Service modules. Once we have these modules enabled, we can go to Administer | Services to learn how to configure the Services module for external communication.
[edit] Services configuration
Once we navigate to Administer | Services, we should see three different tabs at the top of the screen: Browse, Keys, and Settings.
Under the Browse tab, we should see a listing of all Servers installed as well as all the Services available to outside applications. Since we have enabled the System and Node Services, we should see just a system and node section followed by the following routines associated with those services:
-
system.connect -
system.mail -
system.getVariable -
system.setVariable -
system.moduleExists -
node.get(which isnode.loadin Drupal 5) -
node.save -
node.delete
By using these functions, outside applications can connect to your Drupal system and read, write, or manipulate any node in your Drupal website. Although this may raise a red flag of security, keep in mind that much effort has been contributed to the security of the Services module, which is what brings us to the next couple of tabs: Keys and Settings.
[edit] Creating a Services key
If we click on the Keys tab, we should be given two tabs at the top, where we can either list the valid keys in our system, or create a new one. Only the service routines that manipulate data in our Drupal system require the application using that service to provide an API (Application Programming Interface) key before any data manipulation can occur. The concept of a key is rather simple. When we create a new key, the Services module will create a randomly generated string. Then, we can provide this string within our external application to call certain routines that manipulate data. This is just a way of protecting our site from malicious spam bots bombarding our server with service calls and attempting to manipulate our node data automatically. So, let's go ahead and create a new key by clicking on the Create Key tab.
Once we click on this tab, we will be given the option to enter text in two different fields: the Application title, and the Allowed domain.
The Application title can be anything, so we will enter something descriptive about the application we are building, which will be Hello World for our example. The next field is an important one.
The Allowed domain will be used to populate a cross-domain file on your server that specifies the domains allowed to access the services on our server. Since we are using a local server to test our application, we can provide the domain name of our localhost plus any subdirectory where our Drupal root is located. For our example, this will simply be localhost/drupal6.
When we are done with entering these values, we can click on the Create Key button to create our key. After our key has been created, we should see our new key added to the list of valid keys to be used by external applications as shown in the following screenshot:
Now that we have an API key, we are ready to move on to the Settings tab.
[edit] Services settings
The Settings tab of the Services administrator is used to enable or disable certain security measures as well as increase the timeout used when calling routines that require an API key. The only thing that is important to note here is that you should always have both the Use keys and the Use sessid (Session Handling) checked at all times when dealing with Services. Otherwise, you run the risk of forgetting to re-enable them later, which will introduce a security risk to your site. It also forces you to write your applications properly so that they will never compromise with the security of your data.
So, now that we have the Services configured, our next step is to explore the user permissions used to control access of the web services to different user roles in your Drupal system.
[edit] Service Permissions
Before we can start using our web service routines to build our Hello World application in Flash, it is also very important to configure the permissions so that we can access the data that we need from Drupal. We do this by first going to the Administer | Permissions section within the User Settings section of the Drupal Administrator.
Once we are there, we will need to find the following permissions and enable them for all user roles:
|
For Drupal 5, these permissions are labelled different than they are for Drupal 6. If you are using Drupal 5, then you will need to make sure you enable the load raw node data and access services permissions. |
This is a necessary step that will allow outside applications to make service calls without having to log in to our Drupal system as a valid user (we will cover this in later tutorials). It is also important to remember to click the Save User Permissions button at the bottom of the page when we are finished checking all the necessary permissions.
We are now done with the boring setup and configuration of Drupal. Now, on to the fun p
[edit] Additional References
- For instructions on Building Drupal themes, click here
- For instructions on Customizing Drupal Theme, click here
- For Theming Drupal, click here
- For instructions on Setting Up The Development Environment for Drupal 6, click here
- For instructions on Troubleshooting Drupal, click here
- For instructions on Customizing Drupal 6 Themes , click here
- For instructions on Advanced Theming on Drupal Multimedia , click here
- For instructions on Creating and Customizing Drupal 6 Themes, click here
- For instructions on Theming Drupal6, click here
- For instructions on Drupal, click here
- For instructions on Theming Drupal 5 Views Recipes, click here
- For instructions on Installing Drupal Views Module , click here
[edit] Source
The source of this content is Chapter 2: Setting up Drupal for web services of Flash with Drupal by Travis Tidwell (Packt Publishing, 2009).
