views:

372

answers:

3

Hi,

I'm writing a PHP web service and one function. I want to set up a web service in PHP. I need to generate the WSDL description for this web service so it's accessible from I.e. visual studio. It takes documents / search strings as inputs and recommends similar documents as output. I return an array with a first element resultCode (int) which shows if the operation was a success (1) or a failure (0). The second element, however, could either be an error message (string) which tells the user what went wrong, or a complex return type like an array with subelements for the different matching articles, i.e. array( array("heading"=>"article heading", "articleId"=>12345, "text"=>"body text of article"), array( ... ), ... ). I need to know how to generate/write the WSDL for that return type or how to do that in NuSOAP. How would you do that?

This is some of the code that I'm currently using to set up the service.

$server->wsdl->addComplexType(
'returnStructBase',
'complexType',
'struct',
'all',
'',
array('resultCode' => array('name'=>'resultCode', 'type'=>'xsd:int'),
   'resultData' => array('name'=>'resultData', 'type'=>'xsd:anyType')
  )
); 
$server->wsdl->addComplexType(
'returnStructArray',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
 array('ref' => 'SOAP-ENC:arrayType',
    'wsdl:arrayType' => 'tns:returnStructBase[]'
  )     
 ),
'tns:returnStructArray'
);

$server->register("GetRecommendations", array('username'=>'xsd:string', 'password'=>'xsd:string','articleId'=>'xsd:string',
    'text'=>'xsd:string', 'returnText'=>'xsd:boolean'), array('return'=>'tns:returnStructArray'), $namespace, $namespace . '#getRecommendations', 'rpc', 'encoded', ' ... ');

Maybe PHP's loose typing made me use a bad design for a return type and I need to use something else?

Any recommendations welcome.

+1  A: 

You could use xsd:anytype. In fact I'd not recomend it, since type based environments like .NET and Java will not be able to handle your wsdl.

To get a clean solution I'd rethink that design php seduced you to... ;)

KB22
A: 

You should always return the same structure. In the case of a failure you should use exceptions just like in normal usage by using SOAP faults: http://www.ibm.com/developerworks/webservices/library/ws-tip-jaxrpc.html

tster
A: 

You can return

  • First element: Error code, 0 = bad, 1 = good
  • Second element: Error message, empty if we are good
  • Third element: your complex type, empty one if we are bad.
FractalizeR