Drupal – Send SOAP Request by NuSOAP

In PHP, we can use cURL library for generating SOAP request.
PHP – Send a SOAP Request by cURL

Except using the cURL library, we can also use the NuSOAP. The NuSOAP provides much more convenient way to create the SOAP request and it can be used in Drupal. The following example is base on the Blog post written be Eric London.
Executing a SOAP call from Drupal using nusoap

1. Create the nusoap folder in <drupal_dir>/sites/all/modules.

2. Download the NuSOAP, extract the lib folder and upload it to <drupal_dir>/sites/all/modules/nusoap.

3. Create the <drupal_dir>/sites/all/modules/nusoap/soap-server.php

<?php
  // define namespace
  define('NUSOAP_NAME_SPACE', 'erl.dev');

  // define path to nusoap library file
  $nu_soap_path = 'lib/nusoap.php';

  // ensure nu_soap library exsists
  if (!file_exists($nu_soap_path)) {
    die('An error has occurred initializing the soap server.');
  }

  // include nu_soap library
  require_once ($nu_soap_path);

  // create new soap server instance
  $soap_server = new nusoap_server();

  // configure wsdl
  $soap_server->configureWSDL(NUSOAP_NAME_SPACE, 'urn:'. NUSOAP_NAME_SPACE);

  // add a custom data type: person
  $soap_server->wsdl->addComplexType(
    'person',
    'complexType',
    'struct',
    'all',
    '',
    array(
      'firstName' => array(
        'name' => 'firstName',
        'type' => 'xsd:string',
      ),
      'lastName' => array(
        'name' => 'lastName',
        'type' => 'xsd:string',
      ),
    )
  );

  // register method: personTransfer
  $soap_server->register(
    // method name
    'personTransfer',
    // input args
    array('person' => 'tns:person'),
    // output args
    array('return' => 'tns:person'),
    // namespace
    'uri:'. NUSOAP_NAME_SPACE,
    // SOAPAction
    'uri:'. NUSOAP_NAME_SPACE .'#personTransfer',
    // style
    'rpc',
    // use
    'encoded'
  );

  // process raw post data
  $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  $soap_server->service($HTTP_RAW_POST_DATA);

  /**
   * Define soap methods
   */
  function personTransfer($person = array()) {
    // per testing, modify data
    foreach ($person as $key => $value) {
      $person[$key] = $value . "!";
    };
    return $person;
  }
?>

 

4. Now you can access the WSDL and the Web Service details @ http://<drupal>/sites/all/modules/nusoap/soap-server.php

5. Create the <drupal_dir>/sites/all/modules/nusoap/nusoap.info

; Reference: http://thedrupalblog.com/executing-soap-call-drupal-using-nusoap
name = NuSOAP Example
description = NuSOAP example created by Eric London
core = 6.x

 

6. Create the <drupal_dir>/sites/all/modules/nusoap/nusoap.module.

<?php
  // define namespace
  define('NUSOAP_NAME_SPACE', 'erl.dev');

  /**
   * Implements hook_perm()
   */
  function nusoap_perm() {
    return array('access soap');
  }

  /**
   * Implements hook_menu()
   */
  function nusoap_menu() {

    $items = array();
 
    $items['soap-client'] = array(
      'title' => t('Soap client'),
      'description' => t('Soap client'),
      'page callback' => 'nusoap_page_callback_soap_client',
      'access arguments' => array('access soap'),
      'type' => MENU_CALLBACK
    );
 
    return $items;
  }

  /**
   * Implements custom page callback for soap client
   */
  function nusoap_page_callback_soap_client() {

    // include nu_soap library
    require_once(drupal_get_path('module', 'nusoap') .'/lib/nusoap.php');

    // define wsdl path
    $wsdl_path = 'http://' . $_SERVER['HTTP_HOST'] . base_path() . drupal_get_path('module', 'nusoap') . '/soap-server.php?wsdl';

    // create new soap client instance
    $soap_client = new nusoap_client($wsdl_path, true);

    // check for error
    $error = $soap_client->getError();
    if ($error) {
      // handle error
    }

    // define method arguments
    $args = array(
      'person' => array(
      'firstName' => 'Eric',
      'lastName' => 'London'
    )
  );

  // call soap server method
  $result = $soap_client->call('personTransfer', $args);

  // debug output:
  $output = "";
  $output .= "<pre>";
  $output .= "SENT: ";
  $output .= print_r($args, true);
  $output .= "RECEIVED: ";
  $output .= print_r($result, true);
  $output .= "</pre>";
   
  return $output;

}
?>

 

7. Enable the module now

 

8. Try the SOAP request now by typing http://<durpal>/soap-client

 

The NuSOAP works perfectly, Thanks Eric for his clear instructions.

If the SOAP body contains UTF8 character, we need to turn of decoding for UTF8 in the SOAP client. For example, i added Chinese character in the person object.

...
    // define method arguments
    $args = array(
      'person' => array(
      	'firstName' => '英傑',
      	'lastName' => '袁'
      )
    );
...

 

So if we send this object to server, both request and response could not be shown properly.

 

Add $soap_client->decodeUTF8(false); in the SOAP client inside nusoap.module could solve the problem.

...
    // create new soap client instance
    $soap_client = new nusoap_client($wsdl_path, true);

    // fix UTF8 decode problem
    $soap_client->decodeUTF8(false);

    // check for error
    $error = $soap_client->getError();
    if ($error) {
      // handle error
    }
...

 

The Chinese characters could be displayed now.

 

Reference:

13 thoughts on “Drupal – Send SOAP Request by NuSOAP”

    1. You are welcome. Kudos to Eric London as i just follow his post. Anyway, good to know that it could help. =)

      Like

  1. hello, i have a case about uncompatible nusoap in windows, In windows XP sp2 i testing my script and run it normaly, but when i test in windows 7 i not get any result like in windows xp sp2, you ever get this case? if yes please share.. thanks..

    Like

      1. if i use ApacheFriends XAMPPversion 1.6.5 (windows 7) i dont have any error -just blank page-, the page just like looping infinitely.
        If i using ApacheFriends XAMPPversion 1.7.4 (windows 7) i get error : XML error parsing SOAP payload on line 1: Not well-formed (invalid token).
        therefore i choose ApacheFriends XAMPPversion 1.6.5 because my old script run normally.

        Like

      1. Do you have notepad++? open your file like the soap-server.php and check the encoding as follow

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.