Last updated April 6, 2011. Created on April 2, 2011.
Edited by amalaer. Log in to edit this page.

Introduction

This section describes by example how to consume a SOAP web service using the Web service client module from within Rules.

What

We use a temperature conversion example:

  1. User input: a temperature value in Celsius
  2. Temperature conversion to Fahrenheit by web service (http://www.w3schools.com/webservices/tempconvert.asmx)
  3. Display value in Fahrenheit

(See illustration.)

How

  1. Add a web service description
  2. Node form (create node) for user input
  3. Workflow with Rules (screenshot)
  • Event: after saving new content
  • Condition: content is of given type
  • Action: create a data structure
  • Action: call web service (using Web service client)
  • Action: display result

Requirements

  • Drupal 7 (time of writing: 7.0)
  • Rules modules (time of writing: 7.x-2.0-beta1)
    • Rules
    • Rules UI
  • Web service client modules (time of writing: 7.x-1.0-alpha3)
    • Web service client
    • Web service client SOAP
    • Web service client UI
  • PHP filter (time of writing: 7.0) optional: will be mentioned where needed

Instructions

(All screenshots as a gallery.)

  1. Administration -> Configuration -> Web services -> Web service descriptions -> Add web service description (screenshot)

    Web service description saved without errors (screenshot)

  2. Add content type "Celsius to Fahrenheit" with a float field for the temperature input (screenshot)
  3. Administration -> Configuration -> Workflow -> Rules -> Add new rule: "Celsius to Fahrenheit" on event "After saving new content" (screenshot)
  4. Administration -> Configuration -> Workflow -> Rules -> Editing reaction rule "Celsius to Fahrenheit"
    • Add condition: Content is of type "Celsius to Fahrenheit" (screenshot)
    • Add a new action: Create a data structure
      • Select the action to add: Data > Create a data structure (screenshot)
      • TYPE: Value: "Temperature Converter: CelsiusToFahrenheit" (screenshot)
      • Continue
      • CELSIUS: Value: [node:field-ctf-temp] (the name of the temperature input field) (screenshot)
      • Provided variables: CREATED DATA (screenshot)
        • Variable label: Celsius input
        • Variable name: celsius_input
      • Save
    • Add a new action: "Temperature Converter: CelsiusToFahrenheit"
      • Select the action to add: Web services > "Temperature Converter: CelsiusToFahrenheit" (screenshot)
      • Set parameters and names (screenshot)
        • PARAMETERS: Data selector: celsius-input (the name given in the data structure)
        • Provided variables: CELSIUSTOFAHRENHEITRESPONSE
          • Variable label: CelsiusToFahrenheitResponse
          • Variable name: fahrenheit_output
        • Save
    • Add a new action: Show a message on the site (Option 1: Data selector only)
      • Select the action to add: System> Show a message on the site (screenshot)
      • MESSAGE: Data selector: "fahrenheit-output:CelsiusToFahrenheitResult" (the name of the web service output) (screenshot)
      • Save
    • Add a new action: Show a message on the site (Option 2: Data selector with PHP evalution (needs PHP filter module))
      • Select the action to add: System> Show a message on the site (screenshot)
      • Message using Data selector with PHP evalution (needs PHP filter module) (screenshot)
        • MESSAGE: Data selector: "fahrenheit-output:CelsiusToFahrenheitResult"
        • MESSAGE: PHP EVALUATION: return $value . ' Fahrenheit'; (without <?php ... ?> tags)
    • Add a new action: Show a message on the site (Option 3: Direct input with PHP evalution (needs PHP filter module))
      • Select the action to add: System> Show a message on the site (screenshot)
      • Message using Direct input with PHP evalution (needs PHP filter module) (screenshot)
        • MESSAGE: Value:
          <?php
          // var_dump($celsius_input); // do this to learn about the variable at hand
          // var_dump($fahrenheit_output);
          echo $celsius_input['Celsius'] . ' Celsius is ' . $fahrenheit_output->CelsiusToFahrenheitResult . ' Fahrenheit';
          ?>
        • MESSAGE: PHP EVALUATION: see variable names
  5. Complete rule "Celsius to Fahrenheit" (screenshot)
    • Events
      • After saving new content
    • Conditions
      • Content is of type "Celsius to Fahrenheit"
    • Actions
      • Create a data structure "Temperature Converter: CelsiusToFahrenheit"
      • Call web service "Temperature Converter: CelsiusToFahrenheit"
      • Show a message on the site option "data selector"
      • Show a message on the site option "data selector with PHP"
      • Show a message on the site option "direct input with PHP"
  6. Create content of type "Celsius to Fahrenheit" and celebrate :-) (screenshot)

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

mvonline’s picture

Hi
I have An input parameter with "unsignedbyte" Type;
In step 4 When I want to describe related input Field To this parameter i have a problem and cant do that.
what can I do ;
please answer To this Question

colinsjones’s picture

I'm trying to use the WSClient module to consume a remote SOAP web service on Drupal 7.2 and I'd like to view the SOAP XML before it gets dispatched to the server. Can any kind person suggest how I can do that?

I've looked at the module code and can't see a variable name I could print.

Thanks
Colin Jones

colinsjones’s picture

You can see the SOAP XML by calling a PHP5 SOAP client method __getLastRequest(), after the call has been made (very useful for debugging SOAP client code).

fhojman’s picture

I get the following messages when I try to save the URL (step 1 of the instructions above):

Warning: file_get_contents() [function.file-get-contents]: URL file-access is disabled in the server configuration in wsclient_soap_wsclient_service_form_validate() (line 100 of /homepages/17/d350495749/htdocs/Drupal/sites/all/modules/wsclient/wsclient_soap/wsclient_soap.module).
Warning: file_get_contents(http://www.w3schools.com/webservices/tempconvert.asmx?WSDL) [function.file-get-contents]: failed to open stream: no suitable wrapper could be found in wsclient_soap_wsclient_service_form_validate() (line 100 of /homepages/17/d350495749/htdocs/Drupal/sites/all/modules/wsclient/wsclient_soap/wsclient_soap.module).
Error downloading the WSDL file.

Can someone help ?

Thanks.

gm03’s picture

i have done with the above given process & created the content of type "Celsius to Fahrenheit" but not getting any output, please help I am new to drupal

gm03’s picture

I am getting the following error :

Status messageError
Error Fahrenheit
[node:field_ctf_temp] Celsius is Error Fahrenheit
Celsius to Fahrenheit name has been created.

According to me there is some error in retreiving value from text filed coz when I directly pass the value in call function in WS_client.module file, it shows the result as follows :

Status message98.6
98.6 Fahrenheit
[node:field_ctf_temp] Celsius is 98.6 Fahrenheit
Celsius to Fahrenheit new has been created.

The code m changing is :

public function call($operation, $arguments) {
$arguments["parameters"]["Celsius"] = 37;
//print_r($arguments);

$client = $this->client();
try {
$response = $client->__soapCall($operation, $arguments);

return $response;
}
catch (SoapFault $e) {
throw new WSClientException('Error invoking the SOAP service %name, operation %operation: %error', array('%name' => $this->service->label, '%operation' => $operation, '%error' => $e->getMessage()));
}
}
}

cspbm’s picture

I'm calling a web service that uses complex data types as well as requires authentication by passing a user name and password. I have the data structure setup and can make the call but can't seem to tie in the user name and password into it. Can you point me in the right direction, please?

afabian’s picture

Can anyone have some information about this topic?

millerengage’s picture

I was able to edit wsclient_soap/wsclient_soap.module to incorporate authentication using SOAP headers. Here is the code I have changed:

<?php
 
public function call($operation, $arguments) {
   
$client = $this->client();
   
    if (
$this->service->name == "my_web_service" ) {
       
$auth = array("user"=>"username", "password"=>"password");
       
$header = new SoapHeader('NAMESPACE', 'auth', $auth, false);
       
$client->__setSoapHeaders($header);
    }
    try {
    ...
  }
?>

You will have to get the namespace from the WSDL and check which header values need to be set.

alerivrod’s picture

Hi guys,

I developed this example easily with the SOAP architecture. Afterwards, I tried with REST arquitectures using some example webservices, given in the module Web service client examples, but im not able to do it. It just ignores the output. I follow the same proccess than for the Celsius Fahrenheit example. Do u know if that should work??

Thank u in advance.
Alejandro.

alerivrod

simoneaudesigns’s picture

Hi, so I am consuming a web service, and I am able to display the content in a message, but how can I display this same content on a page, without the message? Thanks in advance for your help!

Java Experience’s picture

if looking for basic java based consumption of soap web service see:
http://www.javaexperience.com/generate-soap-web-service-client-using-wsi...

Anonymous’s picture

not as show after consuming web services from another site, I am very clear example if someone has another example to appreciate

Horroshow’s picture

Can't complete the example. The screenshot in step #1 shows that you need to input Data Types but no detail in the description.

And in #4, after adding the 'Create a data structure' rule, there's no CELSIUS value field where to put the token [node:field-ctf-temp].

DeN_’s picture

Thank you for this article really helpful info.

How i can output information from WSDL into existing CCK fields or simply with PHP in templates without or within Rules module?

calello’s picture

Thank you for the clarifying tutorial!

If I create another field in the "Celsius to Fahrenheit" content type with a float field for the temperature output, is it possible to save the result of the conversion in it?

Thank you in advance.

MC