Drupal 7 as a Web Service Using PHP

Drupal is an open source content management system (CMS) and as such, can be useful as a back-end supporting system for other applications, mobile, web or any other. In order to connect between various applications and the Drupal system, we need to construct some kind of web service functionality. This will enable us to easily send and retrieve information to and from the Drupal site.
In this two part tutorial I will describe how to create a simple XML based web service using Drupal 7, however this can be easily done on other Drupal versions like Drupal 6.

Part 1: Create an access point to the Drupal system

Every web service needs some point of entry that will enable external systems a connection to the back-end system. Creating this kind of access point on the Drupal platform is done using the hook_menu() function.
First, it is best to create your own module where you will write your custom PHP code. Let's call this module "mymodule". So the file be named "mymodule.module" and inside that file, we will implement the hook_menu() function like so:

The above code will enable external applications to access the Drupal system using a simple HTTP GET or POST call to the "http://www.domain.com/ws" URL. Once this URL is called, the request will be processed by a function named "mymodule_execute_ws_call" which is located in the "ws.inc" file. This leads us to the next part of this tutorial: How to handle HTTP requests.

Part 2: How to handle HTTP requests

Inside a file named "ws.inc" we will introduce a function called "mymodule_execute_ws_call". This function will check the request arguments and look for a parameter named "op". According to the value of that parameter, the function will parse the request for the remaining required parameters and execute the designated logic.
For example, the external application will call "http://www.domain.com/ws?op=TEST&name=World" and the return XML will be:

This is a very simple example of how to parse parameters from GET or POST requests using PHP and how to return an XML response. Obviously, a more generic solution is needed if we want the Drupal web service we built here to handle numerous types of requests (different "op" argument values). This can be done by building a generic PHP class that represents an XML response, but that is for another tutorial ;)

Comments

It's works but $_POST is empty

$_POST is available only when

Submitted by zenno on Tue, 06/18/2013 - 09:20

$_POST is available only when reaching the page after submitting a form ("POST" method request). If you simply put the page URL in your browser, it is a "GET" method request, not "POST" so you will only have data in $_GET.

$_POST not a method in https services

Submitted by Tài Nguyễn (not verified) on Tue, 07/21/2015 - 05:38

I think $_POST just use for form action, and it not able to use for http request. I mean if we want to catch the response from POST method in web services, we use the http_get_request() functionality from PHP.