Create a Web Service using SOAP

This week one of the requirements for our client was to have a application communicate with an external web service running on PHP to generate a barcode sequence. I’ll teach how I was able to setup the web service using NuSOAP and a document/literal as rcp/encoded is not supported by My goal was to make the task easier on php side so from it would make a request by passing a string delimited by an asterisk and pipe. We process the string and return it back as url for the barcode image. I wont be covering barcode creation though in this tutorial but only the web service and sample client.

e.g. Name*2|Mr.*3

The web service will parse the string convert it to an array. You can ofcourse pass any other type of string with a delimiter.

Here is how the server is.

// Pull in the NuSOAP code
// Create the server instance
$server = new soap_server();
$server->configureWSDL( 'servicename', 'urn:servicename', '', 'document');
'in' => array(
'sf-parameters' => 'xsd:string'),
'out' => array('Pass' => 'xsd:string')
//if in safe mode, raw post data not set:
if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = implode("\r\n", file('php://input'));
$server->service( $HTTP_RAW_POST_DATA);
function myRegister( &$server, $methodname, $params) {
$server->register($methodname, $params["in"], $params["out"],
'urn:servicename', // namespace
$server->wsdl->endpoint .'#'. $methodname, // soapaction
'document', // style
'literal', // use
'Generate Barcode Sequence URL' // documentation
function DoSomething($params) {
$string = $params;
$list = explode("|", $string);
//make associative array
$result = array();
foreach($list as $key=>$values) {
$result[] = explode("*", $values);

//creates the string
$bcStr = "";
foreach($result as $values) {
$bcStr .= $values[0];
for ($i = 1; $i < = $values[1]; $i++) { $bcStr .= "\t"; } } return array('Pass'=> $bcStr);

You are instantiating a class of the NuSOAP server and configuring it with WSDL. You also create two functions,, one handles the $server->register method which takes the argument incoming request and the outgoing response and the $server->wsdl with arguments defining the server as a document/literal.

Then for my task purpose the incoming delimited string is converted to an associative array and then converted back to string with the proper formatting I require ($bcStr), if you have barcode class you can pass that string and a barcode image should be generated from it. That however is not covered on this tutorial.

Next is simple php client to test the web service out.

< ?php // Pull in the NuSOAP code require_once('lib/nusoap.php'); // Create the client instance $ns="urn:servicename"; $client = new nusoap_client('http://localhost/soap_server/hellowsdl2.php?wsdl', true); if ( $client->getError() ) {
print "Soap Constructor Error:


$result = $client->call( "DoSomething", array("parameters"=>$params), $ns);
if ($client->fault) {
print "

Soap Fault:

(". $client->fault->faultcode .")  ".
	$client->fault->faultstring. "

elseif ( $client->getError() ) {
print "

Soap Error:

". $client->getError() ."

else {
print "


". $result["Pass"] ."

print '




' .
htmlspecialchars( $client->request, ENT_QUOTES) .'



' .
htmlspecialchars( $client->response, ENT_QUOTES) .'



' .
htmlspecialchars( $client->debug_str, ENT_QUOTES) .'


This time I instantiated a nusoap_client note that underscore _ as I got stuck on this earlier and got PHP complaining when it was just nusoapclient. Next I created the string params to be passed to the DoSomething method via $client->call.

Then from here we goback to our app we make a soap request to the webservice and it returns with the response we need. On make sure you add your web service the Remote Site Settings under Setup > Administrative Setup > Security Controls.

Kudos for the resource and solution for task from the original author mleiv blog’s.

Post Author: lopau

1 thought on “Create a Web Service using SOAP


    (February 16, 2012 - 2:42 am)

    Hi, I have question about nusoap, did you manage to import existing wsdl into nusoap and map it with php function / class? I try to do it for a couple of days, wihtout success :/

Leave a Reply

Your email address will not be published. Required fields are marked *