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; } ?>
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:
thanks, just what I was looking for!
LikeLike
You are welcome. Kudos to Eric London as i just follow his post. Anyway, good to know that it could help. =)
LikeLike
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..
LikeLike
more :
in windows xp and 7 i use:
– ApacheFriends XAMPP (basic package) version 1.6.5
– PHP Version 5.2.5
– no nusoap core class.
LikeLike
What error did u get?
LikeLike
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.
LikeLike
Could you please check if your files are UTF8 encoded?
LikeLike
i have did it, and my phpinfo is show ISO-8859-1,utf-8;q=0.7,*;q=0.7;
LikeLike
Do you have notepad++? open your file like the soap-server.php and check the encoding as follow

LikeLike
Original article here: http://thedrupalblog.com/executing-soap-call-drupal-using-nusoap
-Eric
LikeLike
Thanks for your post. it works perfectly when i implement it. =)
LikeLike
Hello,
How can I call drupal functions from the soap-server.php
Thanks.
LikeLike
Add the following to the soap-server.php
Reference: Swami Web Design – Call to undefined function db_query()
LikeLike