Question

I need to use Drupal as client for a web service, for extracting data and showing it in a view or as content. (The format use is JSON or REST.)

I use a Web Service Client module in Drupal 7, and the Services module in Drupal 6, but I don't know how to use them. I read the documentation but I don't get any result.

Can anyone help on this issue, or tell me how to use these modules? Is there any other module for this purpose?


A possible solution: I find a module called XML VIEWS but this module convert only XML files.

Now, I programing in this module for convert the JSON to XML.

In this files: xml_views_plugin_query_xml.inc

In this function: Execute

Anyone have another solution?

Was it helpful?

Solution

Drupal has specific functions that can help with communicating with a web service, and handling JSON input.
Drupal 7 has the following functions:

With those functions you can write your own custom module to make requests to a site implementing a web service.

If there are some restrictions about the number of requests that can be done to the web server, you can use the Drupal functions to cache the result obtained from the web server:

To notice that Drupal supports more than one cache bin, and modules can use their own cache bins, when necessary.
Drupal supports also cache items that are automatically removed at the successive cache swipe (see the description for the CACHE_TEMPORARY constant). Implementing hook_flush_caches(), the modules allow to the administrator users to clean the cache used by the module when the "Clear" button on the "Performance" page is clicked, or any times a module calls drupal_flush_all_caches().

In Drupal 7, cache data for frequently used data are associated with a static variable handled with drupal_static(). When the data is really frequently used when outputting the same page, code similar to the following one is used:

  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['implementations'] = &drupal_static(__FUNCTION__);
  }
  $implementations = &$drupal_static_fast['implementations'];

  // …

  // Fetch implementations from cache.
  if (empty($implementations)) {
    $implementations = cache_get('module_implements', 'cache_bootstrap');
    if ($implementations === FALSE) {
      $implementations = array();
    }
    else {
      $implementations = $implementations->data;
    }
  }

The code is part of module_implements().

OTHER TIPS

Every time I have needed to consume a webservice, I have just created a PHP class based around file_get_contents or curl in conjunction with json_decode or simplexml_load_string.

The class then gets accessed in a module via a getter function that uses drupal_static, or the $static pattern for Drupal 6.

I then use this inside my custom blocks, menu callbacks, etc, as needed.

EDIT

@kiamlaluno's answer is correct and demonstrates a way to do this all through the Drupal API. The primary reason I don't do this is that I prefer to abstract the webservice class in a way that allows me to use it in other applications. There are advantages and disadvantages to this, so weigh your options.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top