Soap service with Services and phpSoap

Last updated on
30 April 2025

The current Soap server module uses nuSoap. Development of NuSoap has been on hold for sometime now. phpSoap now provides all functions that nusoap provides and is also compatible with most of current Soap standards.

Here is a example of how to create a Soap Server using PHPSOAP and wsdl.

In my case i have a wsdl that holds data structure of members and provides a method sync_mem_profiles ,

Step 1: Tell services that you want to setup a new service

function mymodule_server_info() {
    return array(
    '#name' => 'MyService',
    '#path' => 'myservice'
    );
}

Step 2: define callback function for your SOAP server, this function will be called when requests are received at "/services/myservice"

/**
 * Implementation of hook_server
 * @return unknown_type
 */

function mymodule_server() {
    session_start();
    global $HTTP_RAW_POST_DATA;

    $server = new SoapServer( drupal_get_path('module', 'mymodule') .'/wsdl/mymodule.wsdl');
    ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL after the server is initiated
    $server->setClass('myDataClass); 
    $server->handle($HTTP_RAW_POST_DATA);
}

code $server = new SoapServer( drupal_get_path('module', 'mymodule') .'/wsdl/mymodule.wsdl') instantiates a soap server using wsdl available under /wsdl/ folder within path of mymodule.

the statement $server->setClass('myDataClass') tells soap server that all the business logic, data structures and methods to deal with incoming request are present within a class "myDataClass" .

At this stage all soap requests coming to your soap server at /services/myservice will be read by PHPSOAP and will be passed as method sync_mem_profiles of myDataClass class.

Step 3: Declare the class and associated methods to deal with incoming requests, You can add the code below as a include file and add the include file to your module.

class myDataClass {
    public function UsernameToken( $username, $password ){
        // Store username for logging
        $this->Username = $username;
        $auth = new $this->AuthClass( $username, $password, get_class($this) ); // You will have to Define method AuthClass
        if ( $auth->IsValid() ){
            $this->Authenticated = true;
        } else {
            $this->ThrowSoapFault( 'auth' );
        }
    }

    /**
    * Update profile method for incoming SOAP message
    * @return unknown_type
    */

    function sync_mem_profiles ($message){
	/* Your business logic to deal with incoming message goes here
	* 
	* 
	*/
        $this->msg = "Success";
        return $this;
    } // End of sync_mem_profiles
}

In step3 above, function "UsernameToken" is used to deal with incoming requests that have UserName security tokens enabled.

Help improve this page

Page status: Not set

You can: