Skip to main content

Consume webservice SOAP programmatically with Drupal

Submitted by drw on
webservice

Talk about webservice is talk about versatility and integration of systems for interchange data and operations from other systems.

 

One advantage of Drupal is development API that allow us to work easy way.

Since a time ago in my work we need to work with a query service that consume data from a system based on java (jboss) from a drupal website, my first step was googling a long time and found an example of consuming webservice with rules module from interface user https://www.drupal.org/node/1114308, but this was a little limited for sort and format data get and expose sensible data with access to webservice.

I'll be mentioning the next modules in this post:

In this post, I write about custom module for Drupal using wsclient module

Using hook_default_wsclient_service

The first we will do is implement this hook in our custom module, like we will see in the next example code:
/**
 * Implements hook_default_wsclient_service().
 */
function taxes_default_wsclient_service() {  

  // Taxes from another system (SOAP)
  $ip = '192.168.1.20'; // IP from webservice source
  $port = '8080';  
  
  $service = new WSClientServiceDescription();
  $service->name = 'taxes';
  $service->label = 'Taxes getting from another system';
  $service->url = 'http://' . $ip . ':'. $port . '/paymentPlatform?wsdl';
  $service->type = 'soap';
      
  try {
    $service->endpoint()->initializeMetaData();
    $services[$service->name] = $service;
  }
  catch (WSClientException $e) {
    watchdog('wsclient', $e->__toString());
  }  
  return $services;

  // Here we can call more webservices

}

With this code we can connect to webservice, is mean with IP, Port, webservice Name with their ext WSDL, with this simulate the same thing like we can do from user interface.
http://www.misitio.com/admin/config/services/wsclient
The code I get from the examples that provide the wsclient module, just I change the parameters that work for my environment.

Operations from webservice

For use our webservice we can review the README.txt at the wsclient module where has documentation for developers section.

Usage for developers
--------------------

* You can create web service descriptions in code, see for example
   wsclient_examples.module
* You can easily invoke web services by loading the description and executing
   an operation (the operation name can be used a dynamic method name):

     $service = wsclient_service_load('google');
     $result = $service->translate('Hallo Welt', 'de|en');

Here the module describe how can use wsclient_examples.module for create a webservice description (we watched above).
For call a webservices that we called above, we can do:

$service = wsclient_service_load('taxes');

Here we can be sure the name that sent to the function is the same to defined at begin like $service->name = 'taxes' is mean the name of the webservices, that we mention could be more that one.

After we can use the operations like as:

$result = $service->taxesquery('1100232587');

In the $result variable get the return data from webservice like array, for can see this data we can use the devel module with dsm function like as:
dsm($result);

Types of operations and data from webservices


For more information about who works the operations and parameters mandatories we can using the user interface from http://<www.misitio.com>/admin/config/services/wsclient from here we can see the operations and data types with more details for to know how this work.


I hope this post will be helpful and any comment or feedback is well received. (Sorry for my english)
 

Secciones

Contenido Relacionado