Create a Web Service using SOAP

This week one of the requirements for our client was to have a force.com 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 force.com. My goal was to make the task easier on php side so from force.com 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
require_once('lib/nusoap.php');
// Create the server instance
$server = new soap_server();
$server->configureWSDL( 'servicename', 'urn:servicename', '', 'document');
myRegister($server,'DoSomething',array(
'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:

".
	$client->getError()."

";
}
$params=array("sf-parameters"=>"Name*2|Mr.*3");
$result = $client->call( "DoSomething", array("parameters"=>$params), $ns);
if ($client->fault) {
//soap_fault
print "

Soap Fault:

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

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

Soap Error:

". $client->getError() ."

";
}
else {
print "

Result:

". $result["Pass"] ."

";
}
print '

Details:


'.
'

Request

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

'.
'

Response

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

'.
'

Debug

' .
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 force.com app we make a soap request to the webservice and it returns with the response we need. On Force.com 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.